Boost logo

Boost :

Subject: Re: [boost] [chrono/date] conversion between concrete dates
From: Howard Hinnant (howard.hinnant_at_[hidden])
Date: 2013-05-09 10:03:33


On May 9, 2013, at 6:16 AM, "Vicente J. Botet Escriba" <vicente.botet_at_[hidden]> wrote:

> Could you share your test program so that I can run it with my implementation and on other platforms?

Don't mind at all, though what I have at this point hardly counts as a good benchmark. It is more like back-of-the-envenlope testing:

    // time encoding // about 3-5 clock cycles, 1.2 ns
    const int Ymin = 1900;
    const int Ymax = 2100;
    volatile int k;
    int count = 0;
    auto t0 = std::chrono::high_resolution_clock::now();
    for (int y = Ymin; y <= Ymax; ++y)
    {
        bool is_l = is_leap(y);
        for (int m = 1; m <= 12; ++m)
        {
            int last = db[is_l][m] - db[is_l][m-1];
            for (int d = 1; d <= last; ++d)
            {
                k = days_from(y, m, d);
                ++count;
            }
        }
    }
    auto t1 = std::chrono::high_resolution_clock::now();
    typedef std::chrono::duration<float, std::nano> sec;
    auto encode = t1 - t0;
    std::cout << encode.count() / count << '\n';
    std::cout << sec(encode).count() / count << '\n';

and:

    // time decoding // about 51 clock cycles, 18 ns
    const int Ymin = 1900;
    const int k0 = days_from(Ymin, 1, 1);
    const int Ymax = 2100;
    const int k1 = days_from(Ymax, 12, 31);
    volatile int y;
    volatile int m;
    volatile int d;
    int count = 0;
    auto t0 = std::chrono::high_resolution_clock::now();
    for (int k = k0; k <= k1; ++k)
    {
        std::tie(y, m, d) = ymd_from(k);
    }
    auto t1 = std::chrono::high_resolution_clock::now();
    typedef std::chrono::duration<float, std::nano> sec;
    auto decode = t1 - t0;
    std::cout << decode.count() / (k1-k0+1) << '\n';
    std::cout << sec(decode).count() / (k1-k0+1) << '\n';

The days_from and ymd_from functions simply take int types and do the conversion, with no validity checking. The use of is_leap and db in the encoding tests are taken from my 2011 paper.

I spent some time varying Ymin and Ymax when running these. The current values as shown shouldn't be considered in any way as the "right values" or otherwise special or preferred.

The high_resolution_clock on my platform is pretty good: ns resolution and low overhead. I also experimented with a clock based on the x86 rdtsc instruction and the results were consistent.

Next time I work on this, I'll probably add unit specifiers and see if I can measure a difference, and then validity checking in the encoding (but not decoding) algorithm. It might also be nice if I subtract out the looping overhead from each of the tests.

Howard


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk