Boost logo

Boost Users :

From: 张沈鹏 (zsp007_at_[hidden])
Date: 2006-09-30 14:49:44


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;
}




Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net