How to find out how long a function takes to implement

May 7, 2017 at 10:36pm
I have written 4 different algorithms that implement the same mathematical functional in different ways, the first is supposed to be slowest as it uses multiple layers of nested loops, the last one is supposed to be the fastest as it uses no nested loops and only uses integer maths.

I want to find out the actual processing duration of these functions for a given 2D array and also find out how this duration changes as I increase the size of the 2D array.

I am using C++ in the visual studio 2015 community version.
Last edited on May 7, 2017 at 10:38pm
May 8, 2017 at 1:38am
Just check the time when you start and end.
Here is a reference
// http://en.cppreference.com/w/cpp/chrono/steady_clock


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
#include <iostream>
#include <chrono>
#include <ctime>
using namespace std;

int main()
{
    
    const auto start = std::chrono::steady_clock::now();
    // code goes under here
   
    // example runs about 10 seconds
    for (int i =0; i < 50000; i++)
    {
    	cout << i << ", ";
    }
    // End code above this
    const auto end = std::chrono::steady_clock::now();
    const auto c_time = std::time(nullptr) ;
    const auto elapsed_time = end - start ;
    const auto elapsed_seconds = std::chrono::duration_cast<std::chrono::seconds>(elapsed_time).count() ;
    std::cout << "finished computation at " << std::ctime( &c_time ) << '\n'
              << "elapsed real time: " << elapsed_seconds << "s\n" ;
return 0;
}
Last edited on May 8, 2017 at 1:40am
May 8, 2017 at 2:11am
The clocks in std::chrono are unsuitable for measuring single-threaded performance; they measure the wrong time.

The right clock to use for this is std::clock()

More information: http://www.cplusplus.com/forum/beginner/146620/#msg771119

Something like this:

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
#include <iostream>
#include <ctime>
#include <algorithm>

// return the processor time (milliseconds) taken to execute the function
template < typename FN, typename... ARGS >
double time_it( FN&& fn, ARGS&&... args )
{
    const auto start = std::clock() ;

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

    const auto end = std::clock() ;

    return (end-start) * 1000.0 / CLOCKS_PER_SEC ;
}

bool foo( long long arr[], std::size_t sz )
{
    for( std::size_t i = 0 ; i < sz ; ++i ) arr[i] += i*i%2 ;
    std::sort( arr, arr+sz ) ;
    return std::is_sorted( arr, arr+sz ) ;
}

int main()
{
    const std::size_t N = 10'000'000 ;
    static long long a[N] {} ;

    std::cout << time_it( foo, +a, N ) << " milliseconds\n" ;
}

http://coliru.stacked-crooked.com/a/32504400ecbeb85b
http://rextester.com/OVFN27746
May 8, 2017 at 4:39pm
does the community version have any optimization allowed? Without, you can get funny answers ...
May 9, 2017 at 2:51pm
The C++ compiler (both microsoft and clang++) is the same in all editions of Visual Studio.
Topic archived. No new replies allowed.