What is the best way to find out how long a function has been running in C ++?

In C #, I would run the Stopwatch class to make some quick and dirty deadlines for how long certain methods take.

What is equivalent to this in C ++? Is there a built-in timer of high accuracy?

+5
source share
9 answers

I used a timer for such situations before: in fact, I had a class with two different implementations: one for Windows and one for POSIX.

The reason was because Windows has a feature QueryPerformanceCounter()that gives you access to very accurate clocks that are ideal for such timings.

POSIX, , , boost.datetime , . , undefined .

+4

Python time_it. , , . , . . :

#include <ctime>

double clock_diff_to_sec(long clock_diff)
{
    return double(clock_diff) / CLOCKS_PER_SEC;
}

template<class Proc>
double time_it(Proc proc, int N=1) // returns time in microseconds
{   
    std::clock_t const start = std::clock();
    for(int i = 0; i < N; ++i)
        proc();
    std::clock_t const end = std::clock(); 
    if(clock_diff_to_sec(end - start) < .2) 
        return time_it(proc, N * 5); 
    return clock_diff_to_sec(end - start) * (1e6 / N);
}

time_it STL:

void dummy_op(int i)
{
    if(i == -1)
        std::cout << i << "\n";
}

template<class Container>
void test(Container const & c)
{
    std::for_each(c.begin(), c.end(), &dummy_op);
}

template<class OutIt>
void init(OutIt it)
{
    for(int i = 0; i < 1000; ++i)
        *it = i;
}

int main( int argc, char ** argv )
{
    {
        std::vector<int> c;
        init(std::back_inserter(c));
        std::cout << "vector: " 
                  << time_it(boost::bind(&test<std::vector<int> >, c)) << "\n";
    }      
    {
        std::list<int> c;
        init(std::back_inserter(c));
        std::cout << "list: "
                  << time_it(boost::bind(&test<std::list<int> >, c)) << "\n";
    }
    {
        std::deque<int> c;
        init(std::back_inserter(c));
        std::cout << "deque: " 
                  << time_it(boost::bind(&test<std::deque<int> >, c)) << "\n";
    }
    {
        std::set<int> c;
        init(std::inserter(c, c.begin()));
        std::cout << "set: " 
                  << time_it(boost::bind(&test<std::set<int> >, c)) << "\n";
    }
    {
        std::tr1::unordered_set<int> c;
        init(std::inserter(c, c.begin()));
        std::cout << "unordered_set: " 
           << time_it(boost::bind(&test<std::tr1::unordered_set<int> >, c)) << "\n";
    }    
}

, - , , ( VS2008 ):

vector: 8.7168

: 27.776

deque: 91.52

: 103.04

unordered_set: 29.76

+3

boost:: timer . . :

boost::timer myTimer;
doOperation();
std::cout << myTimer.elapsed();

P.S. , , . , . -, , . , 1000 , 1000.

+3
+2

++, . . .

+1

- - ( Github). - , , , , Windows Linux, .

( ), , ( : 1), ( : 100). ( ):

// Example that times the compare-and-swap atomic operation from C++11
// Sample GCC command: g++ -std=c++11 -DNDEBUG -O3 -lrt main.cpp microbench/systemtime.cpp -o bench
#include "microbench/microbench.h"

#include <cstdio>
#include <atomic>

int main()
{
    std::atomic<int> x(0);
    int y = 0;

    printf("CAS takes %.4fms to execute 100000 iterations\n",
        moodycamel::microbench(
            [&]() { x.compare_exchange_strong(y, 0); },  /* function to benchmark */
            100000, /* iterations per test run */
            100 /* test runs */
        )
    );

    // Result: Clocks in at 1.2ms (12ns per CAS operation) in my environment

    return 0;
}
+1

, , .

Windows, 10 16 GetTickCount() GetTickCount64(). .

, , . .

0
#include <time.h>

clock_t start, end;
start = clock();
//Do stuff
end = clock();

printf("Took: %f\n", (float)((end - start) / (float)CLOCKS_PER_SEC));
0

.

RAII, , , , .

:

int main()
{
   trace_elapsed_time t("Elapsed time: %ts.\n");
   usleep(1.005 * 1e6);
} 

:

Elapsed time: 1.00509s.
0
source

All Articles