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.

1
  • Any OS / framework restrictions? (C++ doesn't have a standard way of getting high-resolution timings...) Commented Mar 12, 2011 at 15:29

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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; ... }.

2 Comments

the compiler couldn't recognize <boost/progress.hpp> .I use Netbeans in Ubuntu
Well, you need Boost. Try sudo apt-get install boost-dev or something similar.
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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.