[Date_Time]A better algorithm for from_undelimited_string(string)

It's seems faster than the algorithm now in use. The execute file and project file please see the attach. DateValidate cost : 00:00:17.765546 boost_date_validate cost : 00:00:50.232232 Below is cpp source of the algorithm and test ---------------------------------------------------------------------------------------- #include <boost/lexical_cast.hpp> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <iostream> #include <string> #include <boost/date_time/posix_time/posix_time.hpp> using namespace std; using namespace boost; using namespace boost::posix_time; using namespace boost::gregorian; int create_date() { int year=rand() %200+1900; int month=rand() %20; int day=rand() %40; return year*10000+month*100+day; } void date_validate(int date) { static int daysOfMonth[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 }; int monthDay = date % 10000; if (monthDay >= 1300) throw bad_month(); if (monthDay > 1231) throw bad_day_of_month(); if (monthDay == 229) { int year = date / 10000; if ( (year % 4) != 0) throw bad_day_of_month(); if (year %100 == 0 && year % 16 != 0) throw bad_day_of_month(); } int month = monthDay / 100; int day = monthDay % 100; if (! (day > 0 && day <= daysOfMonth[month]) ) throw bad_day_of_month(); } bool DateValidate (string date) { try { date_validate( lexical_cast<int> (date)); return true; } catch (...) { return false; } } bool boost_date_validate (string date) { try { from_undelimited_string (date); return true; } catch (...) { return false; } } int main() { int n=1000000; ptime createDateBegin (microsec_clock::local_time() ); for (int i=0;i<n;i++) { int r=create_date(); string t=lexical_cast<string> (r); } ptime createDateEnd (microsec_clock::local_time() ); time_duration createTime=createDateEnd-createDateBegin; cout<<"create_date cost : "<<createDateEnd-createDateBegin<<"\n"; createDateBegin=microsec_clock::local_time(); for (int i=0;i<n;i++) { int r=create_date(); string t=lexical_cast<string> (r); DateValidate (t); } createDateEnd=microsec_clock::local_time(); cout<<"DateValidate cost : "<<createDateEnd-createDateBegin-createTime<<"\n"; createDateBegin=microsec_clock::local_time(); for (int i=0;i<n;i++) { int r=create_date(); string t=lexical_cast<string> (r); boost_date_validate (t); } createDateEnd=microsec_clock::local_time(); cout<<"boost_date_validate cost : "<<createDateEnd-createDateBegin-createTime<<"\n"; cin.get(); return 0; }

张沈鹏 wrote:
It's seems faster than the algorithm now in use.
The execute file and project file please see the attach.
DateValidate cost : 00:00:17.765546 boost_date_validate cost : 00:00:50.232232
I won't quibble with your measurements, because I'm aware that this code is not especially efficient. That said, it's essentially been deprecated and I'm planning on replacing it with the code that now underlies the streaming operators. Even so, I'm not sure when I'm going to get to this and it's likely not as efficient as what you have submitted here. Feel like writing an official patch? If you do that I'd be happy to incorporate. Jeff
participants (2)
-
Jeff Garland
-
张沈鹏