Measuring the time taken by a program

I want to evaluate how much time a function in an OpenCV program takes to run and how much time a similar function implemented in c++ will take? Which library and which start time and end time functions that i can use, in which both the library and the time functions are compatible with both OpenCV programs and c++ programs.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// return pair of elapsed processor_time, elapsed wall clock time (both in milliseconds)
template < typename FN, typename... ARGS >
std::pair<double,double> time_it( FN&& fn, ARGS&& ... args )
{
    using wall_clock = typename std::conditional< std::chrono::high_resolution_clock::is_steady,
                                                  std::chrono::high_resolution_clock,
                                                  std::chrono::steady_clock >::type ;
    const auto wc_start = wall_clock::now() ;
    const auto pc_start = std::clock() ;

    std::forward<FN>(fn)( std::forward<ARGS>(args)... ) ; // call the function

    const auto pc_end = std::clock() ;
    const auto wc_end = wall_clock::now() ;

    return { ( pc_end - pc_start ) * 1000.0 / CLOCKS_PER_SEC, // processor time millisecs
             std::chrono::duration_cast<std::chrono::milliseconds>( wc_end - wc_start ).count() } ;
}

http://coliru.stacked-crooked.com/a/babd699c5e6f8c20
Last edited on
> perfect forwarding broken in g++ 5.1 ?

Nope. std::sort in LLVM libc++ is just smarter in exploiting patterns in the sequence to be sorted.

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
#include <iostream>
#include <ctime>
#include <vector>
#include <algorithm>
#include <numeric>
#include <random>
#include <iomanip>

void test_it( std::vector<int>& seq, const char* tag )
{
    const auto start = std::clock() ;
    std::sort( seq.begin(), seq.end() ) ;
    const auto end = std::clock() ;
    std::cout << std::setw(30) << tag << ": " << std::setw(4)
              << (end-start) * 1000.0 / CLOCKS_PER_SEC << " millisecs\n" ;
}

int main()
{
    std::vector<int> seq( 8'000'000 ) ;
    test_it( seq, "all values compare equal" ) ;  

    std::iota( seq.rbegin(), seq.rend(), 0 ) ;
    test_it( seq, "pre-sorted in reverse order" ) ;  
    
    test_it( seq, "already sorted" ) ;  

    std::shuffle( seq.begin(), seq.end(), std::mt19937{} ) ; 
    test_it( seq, "shuffled (repeatable)" ) ;  
    
    std::mt19937 twister{ std::random_device{}() };
    twister.discard( twister.state_size ) ; 
    std::shuffle( seq.begin(), seq.end(), std::move(twister) ) ; 
    test_it( seq, "shuffled (random)" ) ;  
}

echo '======== clang++ ========' && clang++ -std=c++14 -stdlib=libc++ -O3 -Wall -Wextra -pedantic-errors main.cpp && ./a.out
echo '========== g++ ==========' && g++ -std=c++14 -O3 -Wall -Wextra -pedantic-errors main.cpp && ./a.out
======== clang++ ========
      all values compare equal:   30 millisecs
   pre-sorted in reverse order:   60 millisecs
                already sorted:   40 millisecs
         shuffled (repeatable):  870 millisecs
             shuffled (random):  880 millisecs
========== g++ ==========
      all values compare equal:  350 millisecs
   pre-sorted in reverse order:  360 millisecs
                already sorted:  450 millisecs
         shuffled (repeatable):  930 millisecs
             shuffled (random):  940 millisecs

http://coliru.stacked-crooked.com/a/48891a1d9bca8789
Topic archived. No new replies allowed.