1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
#include <chrono>
#include <cstdio>
#include <random>

void
doStuff()
{
    std::random_device                     random;
    std::uniform_real_distribution<double> uniform(-100, 100);

    volatile double foo = 0;
    for (std::size_t i=0; i<1000*1000; ++i) {
        foo += uniform(random);
    }
}

int
main()
{
    std::chrono::high_resolution_clock::time_point  t0;
    std::chrono::high_resolution_clock::duration    elapsed;

    //
    // bench doStuff()
    //
    t0       = std::chrono::high_resolution_clock::now();

    doStuff();

    elapsed = std::chrono::high_resolution_clock::now() - t0;

    //
    // Convert duration into seconds and store it in a double
    //
    double elapsedSeconds
        = std::chrono::duration<double,std::chrono::seconds::period>(elapsed).count();

    printf("Time elapesd in seconds: %5.2lf\n", elapsedSeconds);
}