1

I have finished writing a function and I want to compare time and cpu execution of the function with other function. This is code to calculate time execution but I not sure it accuracy. Do you have accuracy code to calculate time and cpu spending for one function in C++?

//Only time execution. CPU spending?
 #include "stdafx.h"
#include <iostream>
#include <time.h>

using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
    clock_t start, end;
    start = clock();

    for(int i=0;i<65536;i++)
    cout<<i<<endl;

    end = clock();
    cout << "Time required for execution: "
    << (double)(end-start)/CLOCKS_PER_SEC
    << " seconds." << "\n\n";
    return 0;
}
user2408476
  • 41
  • 1
  • 1
  • 9
  • possible duplicate of [How to know calculate the execution time of an algorithm in c++?](http://stackoverflow.com/questions/1949752/how-to-know-calculate-the-execution-time-of-an-algorithm-in-c) – Mat May 22 '13 at 07:46
  • 1
    Since you tagged this Visual-C++, have you tried the Visual Profiler ? (Not available in every version though). – JBL May 22 '13 at 07:48
  • possible duplicate of [How to Calculate Execution Time of a Code Snippet in C++](http://stackoverflow.com/questions/1861294/how-to-calculate-execution-time-of-a-code-snippet-in-c) – Roger Rowland May 23 '13 at 05:01

3 Answers3

6

In C++11 you should use std::chrono::high_resolution_clock which under the hood uses the clock source with the smallest tick period provided by the system:

#include <chrono>

auto start = std::chrono::high_resolution_clock::now();
//...
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
syam
  • 14,701
  • 3
  • 41
  • 65
  • Sorry. I am using visual studio C++ 2010 . What is library that your code need include ? – user2408476 May 22 '13 at 07:53
  • @user2408476: I'm afraid I don't know how well Visual Studio supports C++11 features. If including `` doesn't work, you may want either to use [`boost`](http://www.boost.org/) which provides a `high_resolution_clock` implementation, or Mats Petersson's `QueryPerformanceCounter` solution. – syam May 22 '13 at 07:58
  • is not supported in VC++2010. For high precision time measurements you should use QueryPerformanceCounter. – SChepurin May 22 '13 at 08:21
  • Is this the CPU time or computer time ? – BlueTrin Feb 08 '15 at 19:51
1

For this particular code, I'm pretty sure your code is quite OK.

For short periods, you may need to use more precise timing functions, such as Windows' QueryPerformanceCounter and QueryPerformanceFrequency to get higher precision timing.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
1

The easiest way I know of to get precise time estimates is to use an OpenMP function:

#include <stdio.h>
#include <omp.h>
int main() {
    double dtime = omp_get_wtime();
    foo();
    dtime = omp_get_wtime() - dtime;
    printf("time in seconds %f\n", dtime);
}

In gcc compile with -fopenmp. In visual studio turn on OpenMP support under C++/Language Support. BTW, OpenMP now works in Visual Studio 2012 express. For CPU time profiling you can try http://developer.amd.com/tools-and-sdks/heterogeneous-computing/amd-codeanalyst-performance-analyzer/

  • it's quite absurd to measure time with a parallelization suite, but well: it works. – stefan May 22 '13 at 08:13
  • Why is it absurd? The solution is cross platform, simple, accurate, and even works with threading. What's not to like? –  May 22 '13 at 09:03