Measurement time results in return values ​​of 0 or 0.001

I am trying to use chrono::steady_clockto measure the fractional seconds elapsed between a block of code in my program. I have this code block working in LiveWorkSpace ( http://liveworkspace.org/code/YT1I$9 ):

#include <chrono>
#include <iostream>
#include <vector>

int main()
{
    auto start = std::chrono::steady_clock::now();
    for (unsigned long long int i = 0; i < 10000; ++i) {
       std::vector<int> v(i, 1);
    }
    auto end = std::chrono::steady_clock::now();

    auto difference = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();

    std::cout << "seconds since start: " << ((double)difference / 1000000);
}

When I implement the same idea in my program, like this:

auto start = std::chrono::steady_clock::now();
// block of code to time
auto end = std::chrono::stead_clock::now();

auto difference = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count()

std::cout << "seconds since start: " << ((double) difference / 1000000);

The program will print only the values 0and 0.001. I highly doubt that the runtime for my code block is always equal to 0or 1000microseconds, so this takes into account this rounding and how can I eliminate it to get the correct fractional values?

This is a program for Windows.

+5
source share
2 answers

MSVC2012 , ++ 11 Microsoft . . ++ high_resolution_clock , .

, , boost::chrono QueryPerformanceCounter , :

#include <iostream>
#include <Windows.h>

int main()
{
    LARGE_INTEGER frequency;
    QueryPerformanceFrequency(&frequency);

    LARGE_INTEGER start;
    QueryPerformanceCounter(&start);

    // Put code here to time

    LARGE_INTEGER end;
    QueryPerformanceCounter(&end);

    // for microseconds use 1000000.0
    double interval = static_cast<double>(end.QuadPart- start.QuadPart) / 
                      frequency.QuadPart; // in seconds
    std::cout << interval;
}   
+2

. :

<chrono>. . time_point. . <chrono> . , std::chrono, , , :: clock std::high_resolution_clock ( - ).

, :

std::cout << "seconds since start: " << ((double) difference / 1000000);

, , (, 1000000), , , chrono . , . , ?!

:

, .

chrono . , , :

typedef std::chrono::duration<double> sec;
sec difference = end - start;
std::cout << "seconds since start: " << difference.count() << '\n';

1 , .

time_point . steady_clock::time_point ( ) chrono . , :

auto difference = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count()

, - .count(). , :

std::cout << "seconds since start: " << ((double) difference / 1000000);


std::chrono::steady_clock, QueryPerformanceCounter, . QueryPerformanceCounter.

<disclaimer>

Windows, .

</disclaimer>

struct my_clock
{
    typedef double                             rep;
    typedef std::ratio<1>                      period;
    typedef std::chrono::duration<rep, period> duration;
    typedef std::chrono::time_point<my_clock>  time_point;
    static const bool is_steady =              false;

    static time_point now()
    {
        static const long long frequency = init_frequency();
        long long t;
        QueryPerformanceCounter(&t);
        return time_point(duration(static_cast<rep>(t)/frequency));
    }
private:
    static long long init_frequency()
    {
        long long f;
        QueryPerformanceFrequency(&f);
        return f;
    }
};

, rep a double period 1 . rep period - , . typedef QueryPerformanceCounter duration now().

, :

int main()
{
    auto start = my_clock::now();
    for (unsigned long long int i = 0; i < 10000; ++i) {
       std::vector<int> v(i, 1);
    }
    auto end = my_clock::now();

    auto difference = end - start;
    std::cout << "seconds since start: " << difference.count() << '\n';
}

( ) . std::chrono::steady_clock.

<chrono> . .: -)

+5

All Articles