Only one comment: I think that to perform correctly the calculation of the elapsed time, the frequency scaling of the CPU must be disabled.
Only one comment: I think that to perform correctly the calculation of the elapsed time, the frequency scaling of the CPU must be disabled.
Hi,
i used the clock_t to calculate the run time of another function
i get "0 seconde"?
i say maybe it's in millysecond
i used
i get as the first one '0 seconde"Code:cout << (double(clock() - timestart) / CLOCKS_PER_SEC) * 1000<< " seconds." << endl;
?
Note: there is many simple instruction , as sqrt, min, ....
I used this
I get 0.000 i will be crazy how many instructions give me 0 sec? it's impossible no?Code:clock_t start, stop; start = clock(); // // block of instruction that i want calculate it's running time // stop = clock(); printf("Approx seconds, tenths, hundredths and milliseconds: %.3f\n", ((double)(stop - start) / CLOCKS_PER_SEC));
[QUOTE]Thanks for the reply, but how we disable this frequency ?[/QUOTE]
Three possibilities:
- some BIOS usually permits this configuration
- Linux provides a specific widget
- the easiest way is to perform a number of operations that load the CPU before the timing of the function
In Windows, you can go to power management and set the minimum and maximum cpu frequency to the same value. You can also use the high performance power plan to stop it from using power saving features. I'm not sure how it affects TurboBoost, though. But you can set the min and max to 99% to disable that, I'm pretty sure.
Not really. I think you underestimate how fast computers can be. Throw in a few loops and take the average.
This does not disable power savings. It is a pretty poor way.
but i what i need is just the run time of my function , if i get 0 many timeNot really. I think you underestimate how fast computers can be. Throw in a few loops and take the average.
i can't write it
what you mean here ?Repeat what you want to time several times, then take the average. That way you get an approximation of the "real" time.
did you mean i run my app several time ? because i made that and always i get "0.000"
Code:namespace stdc = std::chrono; auto start = stdc::high_resolution_clock::now(); constexpr int LoopCount = 1000000; for (int i = 0; i < LoopCount; i++) { // My code here } auto end = stdc::high_resolution_clock::now(); std::cout << stdc::duration_cast<stdc::milliseconds>((end - start) / LoopCount).count() << "\n";
Because without the loop, it may be too fast. Generally, execution time under a millisecond contains too much "noise." So when you rerun the code many times, you will see a lot of variation. The best way to "approximate" the real run time is to execute it multiple times and take the average/mean. You should also change the loop count to match your code. Too few iterations will mean that you won't get a good average or you will get 0, and too many and it will take too long to execute.
Refer to my last post on the last page for more details.