Time to run the algorithm

I am trying to execute my algorithm, but the time is always displayed as zero. Here is my code:

steady_clock::time_point start = steady_clock::now();
ReverseArray(list1, 1000000);
steady_clock::time_point end = steady_clock::now();
auto totalTime = end - start;
cout << duration_cast<microseconds>(totalTime).count()<< "\n";
0
source share
3 answers

If you are running Windows, use this resource.

This is good for submicrosecond accuracy. Use this:

LARGE_INTEGER StartingTime, EndingTime, ElapsedMicroseconds;
LARGE_INTEGER Frequency;
QueryPerformanceFrequency(&Frequency); 
QueryPerformanceCounter(&StartingTime);
ReverseArray(list1, 1000000);
QueryPerformanceCounter(&EndingTime);
ElapsedMicroseconds.QuadPart = EndingTime.QuadPart - StartingTime.QuadPart;

ElapsedMicroseconds.QuadPart *= 1000000;
ElapsedMicroseconds.QuadPart /= Frequency.QuadPart;
+1
source

: Windows, , Visual Studio 2013 , , unprecise. - , , , ( 8 , ). , (04/2014) VS (, condition_variable.wait_for() this_thread:: sleep_for()). , Boost, . STL (, Microsoft) , Visual Studio 2013 ( CTP, ).

, - ( , high_precision_clock ), , (GCC, Clang, Boost chrono ). , GCC 4.8 (Coliru):

#include <iostream>

#include <chrono>
#include <vector>
#include <algorithm>

auto my_array = []{ 
        std::vector<long long> values( 100000 );
        long long last_value = 0;
        for( auto& value : values )
            value = last_value++;
        return values;
    }();


void some_work()
{
    for( int i = 0; i < 10000; ++i ) // play with this count 
        std::reverse( begin(my_array), end(my_array) );
}


int main()
{
    using namespace std;
    using namespace chrono;

    auto start = high_resolution_clock::now();
    some_work();
    auto end = high_resolution_clock::now();
    auto totalTime = end - start;
    cout << duration_cast<microseconds>(totalTime).count()<< " microsecs \n";

    return 0;
}

Coliru "95 " some_work().

+2

, :

auto t1 = std::chrono::high_resolution_clock::now();

// Your code to be measured

auto t2 = std::chrono::high_resolution_clock::now();
auto t = std::chrono::duration<double,std::milli>(t2-t1).count();

std::cout << "Time (ms):" << t << std::endl;
+1

Source: https://habr.com/ru/post/1681441/


All Articles