![]() Some processors have cycle clocks that count processor cycles rather than wall clock time, but this gets very system-specific. Unless you are working on a real-time system, there are no hard guarantees as to how long a piece of code takes, including the invocation of the measurement functions themselves. Accuracy is how close the reported values are to the real values. ![]() Note the difference between precision and accuracy: precision is the unit that the values are reported. Where available, clock_gettime(CLOCK_THREAD_CPUTIME_ID) or clock_gettime(CLOCK_PROCESS_CPUTIME_ID) may give better precision. Printf("Total time taken by CPU: %fs\n", (double)(end.tv_sec - _sec) + (end.tv_usec - _usec) / 1e-6) Linux and other Unix-like systems), you can use getrusage, which has microsecond precision. Many platforms have more precise ways to measure wall clock time than time and more precise ways to measure CPU consumption than clock. The C standard library was designed to be implementable on a wide range of hardware, including processors that don't have an internal timer and rely on an external peripheral to tell the time. printf("Total time taken by CPU: %fs\n", (double)total_t / CLOCKS_PER_SEC) You need to use the macro CLOCKS_PER_SEC to convert to real time. It's more commonly related to the frequency of an external timer interrupt - which may be configured in software - or to a historical value that's been kept for compatibility through years of processor evolution. ![]() On most platforms, it is unrelated to the processor speed. The unit of time used by the clock function is arbitrary. Would this be the execution time for the for loop in my code? Am I making some incorrect assumptions here. ![]() So if my CPU was running at 21 MHz and assuming that this was the only thing getting executed, each machine cycle would be approximately equal to 47 nanoseconds so (18294 * 47) = 859818 nanoseconds. The output of the code snippet on my machine is Starting of the program, start_t = 8965 Printf("Total time taken by CPU: %lu\n", total_t ) Printf("End of the big loop, end_t = %ld\n", end_t) Printf("Going to scan a big loop, start_t = %ld\n", start_t) įor(i=0 i< 10000000 i++) //trying to determine execution time of this block Printf("Starting of the program, start_t = %ld\n", start_t) I have some code that tries to determine the execution time of a code block. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |