1

I tried some codes by googling :

clock_t start, end;
start = clock();
//CODES GOES HERE
end = clock();
std::cout << end - start <<"\n";
std::cout << (double) (end-start)/ CLOCKS_PER_SEC;

but the result elapsed time always was 0, even with

std::cout << (double) (end-start)/ (CLOCKS_PER_SEC/1000.0 );

Don't know why but when I get the similar in Java : getCurrentTimeMillis() it works well. I want it to show the milliseconds as maybe the computer compute so fast.

Duc Tran
  • 6,016
  • 4
  • 34
  • 42
  • Any OS / framework restrictions? (C++ doesn't have a standard way of getting high-resolution timings...) – Macke Mar 12 '11 at 15:29

3 Answers3

1

I don't think it's guaranteed that clock has a high enough resolution to profile your function. If you want to know how fast a function executes, you should run it maybe a few thousands times instead of once, measure the total time it takes and take the average.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
1
#include <boost/progress.hpp>

int main()
{
    boost::progress_timer timer;
    // code to time goes here
}

This will print out the time it took to run main. You can place your code in scopes to time several parts, i.e. { boost::progress_timer timer; ... }.

Daniel Lidström
  • 9,930
  • 1
  • 27
  • 35
0

This question is somehow similar to yours: Timing a function in a C++ program that runs on Linux

Take a look at this answer!

Community
  • 1
  • 1
karlphillip
  • 92,053
  • 36
  • 243
  • 426