![]() |
VOOZH | about |
Execution time : The execution time or CPU time of a given task is defined as the time spent by the system executing that task in other way you can say the time during which a program is running. There are multiple way to measure execution time of a program, in this article i will discuss 5 different way to measure execution time of a program.
time() : time() function returns the time since the Epoch(jan 1 1970) in seconds. Header File : "time.h" Prototype / Syntax : time_t time(time_t *tloc); Return Value : On success, the value of time in seconds since the Epoch is returned, on error -1 is returned.
Time taken by program is : 0.000000 sec
The time complexity of the program is O(1) since it only performs a constant number of operations regardless of the input size. The fun() function has a fixed number of iterations, and the time() function used to measure time also takes constant time.
The space complexity of the program is also constant since it only uses a fixed amount of memory for the integer variables start, end, and time_taken, as well as the cout statement to print the output. Hence, the space complexity is O(1).
clock() : clock() returns the number of clock ticks elapsed since the program was launched. Header File : "time.h" Prototype / Syntax : clock_t clock(void); Return Value : On success, the value returned is the CPU time used so far as a clock_t; To get the number of seconds used, divide by CLOCKS_PER_SEC.on error -1 is returned.
Time taken by program is : 0.000001 sec
The time complexity of the provided code is O(1), as it simply measures the time required to execute the function fun() using the clock() function from the time library.
The space complexity of this code is also O(1), as it only declares a few variables on the stack which do not grow with the input size.
gettimeofday() : The function gettimeofday() can get the time as well as timezone. Header File : "sys/time.h". Prototype / Syntax : int gettimeofday(struct timeval *tv, struct timezone *tz); The tv argument is a struct timeval and gives the number of seconds and micro seconds since the Epoch. struct timeval { time_t tv_sec; // seconds suseconds_t tv_usec; // microseconds }; Return Value : return 0 for success, or -1 for failure.
Time taken by program is : 0.000029 sec
clock_gettime() : The clock_gettime() function gets the current time of the clock specified by clock_id, and puts it into the buffer pointed to by tp. Header File : "time.h". Prototype / Syntax : int clock_gettime( clockid_t clock_id, struct timespec *tp ); tp parameter points to a structure containing atleast the following members : struct timespec { time_t tv_sec; //seconds long tv_nsec; //nanoseconds }; Return Value : return 0 for success, or -1 for failure. clock_id : clock id = CLOCK_REALTIME, CLOCK_PROCESS_CPUTIME_ID, CLOCK_MONOTONIC ... etc. CLOCK_REALTIME : clock that measures real i.e., wall-clock) time. CLOCK_PROCESS_CPUTIME_ID : High-resolution per-process timer from the CPU. CLOCK_MONOTONIC : High resolution timer that is unaffected by system date changes (e.g. NTP daemons).
Time taken by program is : 0.000028 sec
chrono : Chrono library is used to deal with date and time. This library was designed to deal with the fact that timers and clocks might be different on different systems and thus to improve over time in terms of precision.chrono is the name of a header, but also of a sub-namespace, All the elements in this header are not defined directly under the std namespace (like most of the standard library) but under the std::chrono namespace.
Time taken by program is : 0.000024 sec