This question already has answers here:
How to Calculate Execution Time of a Code Snippet in C++
(18 answers)
Closed 8 years ago.
I'd like to ask, which method of measuring the time which is required for function to execute is considered the best?
Just curious.
Thanks.
You can try to use rdtsc(); it's accurate, perhaps fastest and cross-platform (but only for x86 architecture). For example, in Windows:
#include <intrin.h>
uint64_t rdtsc()
{
return __rdtsc();
}
Related
This question already has answers here:
How to use a timer in C++ to force input within a given time?
(5 answers)
Closed 6 years ago.
I'm writing a code where the user has to answer the question in 10 seconds or else he will lose, I know that I'm supposed to use threads but the problem is, I don't know how to do the timer, any ideas?
Thanks
You don't need threads.
Assuming time is in ms:
updateLoop(){
timeLeft = startingTime + 10.000 - now;
if (timeLeft <= 0) {
lose=true;
}
render();
}
EDIT: I probably misunderstood the question a bit. This seems to cover your question, as in #Mourad's comment:
How to use a timer in C++ to force input within a given time?
This question already has answers here:
How to determine CPU and memory consumption from inside a process
(10 answers)
Closed 7 years ago.
I have a few threads in my program - run on Windows and write in C++.
How can I know in the end of the running how much CPU time is used by all or one of them?
You can use the GetThreadTimes function: https://msdn.microsoft.com/en-us/library/windows/desktop/ms683237%28v=vs.85%29.aspx
This question already has answers here:
C++ cache aware programming
(10 answers)
Closed 7 years ago.
I am profiling my code on various CPUs running Windows7 and my results so far suggest that I need to tune a buffer size proportional to the machine's L2CacheSize or L3CacheSize. Is there a way to obtain these parameters from C++?
You can use the GetLogicalProcessorInformation function to get that. It returns an array of SYSTEM_LOGICAL_PROCESSOR_INFORMATION structures which contain a CACHE_DESCRIPTOR structure, which provides the cache size information.
This question already has answers here:
How to measure the inner kernel time in NVIDIA CUDA?
(2 answers)
Closed 8 years ago.
I searched a bit but all things I found could only be annotated in CPU code, how could I measure partial time inside kernel between 2 _syncthread() of 1 threadblock? Is it possible?
One approach is to use the clock() or clock64 function as described in the programming guide.
Search the cuda tag on clock64 for additional examples of its usage.
This question already has answers here:
Optimizations for pow() with const non-integer exponent?
(10 answers)
Closed 9 years ago.
I have to raise a number to the power of 1/2.2 which is 0.45454545... many times. Actually I have to do this in a loop. Simple pow/powf is very slow (when I comment out this pow from my loop code it's a loot faster). Is there any way to optimize such operation?
You might give a look at: Optimized Approximative pow() in C / C++
It also includes a benchmark.