Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Techie Delight
Coding made easy
Browse
1. clock()
We can use clock() function provided by Adobe Algorithm Amazon
<time.h> header le to calculate the CPU BFS Binary Search Bit Hacks DFS
time consumed by a task within a C FIFO Google Greedy Hashing Intro
application. It returns clock_t type which JSON LCS LIFO Maze Memoized
Close and accept
https://www.techiedelight.com/find-execution-time-c-program/ 1/8
21/09/2019 How to find the execution time of a C program - Techie Delight
#include <stdio.h>
#include <time.h> // for clock_
#include <unistd.h> // for sleep(
return 0;
}
Download
Run Code
2. time()
This website uses cookies. By using this site you agree to the use of cookies, our policies, copyright terms and
other conditions. Read our Privacy Policy
The <time.h> header also provides time()
function that returns the number of seconds Close and accept
https://www.techiedelight.com/find-execution-time-c-program/ 2/8
21/09/2019 How to find the execution time of a C program - Techie Delight
1 #include <stdio.h>
2 #include <time.h> // for ti
3 #include <unistd.h> // for sl
4
5 // main function to find the exec
6 int main()
7 {
8 time_t begin = time(NULL);
9
10 // do some stuff here
11 sleep(3);
12
13 time_t end = time(NULL);
14
15 // calculate elapsed time by
16 printf("Time elpased is %d se
17
18 return 0;
19 }
Download
Run Code
Output :
3. gettimeofday()
This website uses cookies. By using this site you agree to the use of cookies, our policies, copyright terms and
other conditions. Read our Privacy
The gettimeofday() Policy
function returns the
wall clock time elapsed since the Epoch and
Close and accept
https://www.techiedelight.com/find-execution-time-c-program/ 3/8
21/09/2019 How to find the execution time of a C program - Techie Delight
struct timeval {
long tv_sec; /* seconds */
long tv_usec; /* microseconds */
};
1 #include <stdio.h>
2 #include <sys/time.h> // for ge
3 #include <unistd.h> // for sl
4
5 // main function to find the exec
6 int main()
7 {
8 struct timeval start, end;
9
10 gettimeofday(&start, NULL);
11
12 // do some stuff here
13 sleep(5);
14
15 gettimeofday(&end, NULL);
16
17 long seconds = (end.tv_sec -
18 long micros = ((seconds * 100
19
20 printf("Time elpased is %d se
21
22 return 0;
23 }
This website uses cookies. By using this site you agree to the use of cookies, our policies, copyright terms and
other conditions. Read our Privacy Policy
Download
Run Code
Close and accept
https://www.techiedelight.com/find-execution-time-c-program/ 4/8
21/09/2019 How to find the execution time of a C program - Techie Delight
4. clock_gettime()
We can also use clock_gettime() function
de ned in <time.h> header le which
supports upto nanosecond accuracy. It takes
two arguments – the rst arugment is clock
type and the second argument is a pointer to
timespec structure. The timespec structure
is provided by the <time.h> header and is
declared as:
struct timespec {
time_t tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};
1 #include <stdio.h>
2 #include <time.h> // for clock
3 #include <unistd.h> // for sle
4
5 #define BILLION 1000000000.0;
6
7 // main function to find the exec
This website
8 uses cookies. By using this site you agree to the use of cookies, our policies, copyright terms and
int main()
other conditions.
9 { Read our Privacy Policy
10 struct timespec start, end;
11 Close and accept
12 clock_gettime(CLOCK_REALTIME,
https://www.techiedelight.com/find-execution-time-c-program/ 5/8
21/09/2019 How to find the execution time of a C program - Techie Delight
13
14 // do some stuff here
15 sleep(3);
16
17 clock_gettime(CLOCK_REALTIME,
18
19 // time_spent = end - start
20 double time_spent = (end.tv_s
21 (end.tv_n
22
23 printf("Time elpased is %f se
24
25 return 0;
26 }
Downloa
d
Please note that the clock_gettime()
function will work only on very few UNIX
machines.
Related Post:
(4 votes, average: 5.00 out of 5)
https://www.techiedelight.com/find-execution-time-c-program/ 6/8
21/09/2019 How to find the execution time of a C program - Techie Delight
with us.
Like us? Please spread the word and help us
grow. Happy coding
Sharing is caring:
Misc
Leave a Reply
b i link b-quote u ul ol li
code spoiler
Subscribe most
newest oldest
This website uses cookies. By using this site you voted
agree to the use of cookies, our policies, copyright terms and
other conditions. Read our Privacy Policy
https://www.techiedelight.com/find-execution-time-c-program/ 7/8
21/09/2019 How to find the execution time of a C program - Techie Delight
kkk S
a
ys “Coding made easy” but uses
a blocker in the page so people
Guest
can’t select and copy content.
Nice.
0 Reply
6 months ago
Admin
0 Reply
6 months ago
This website uses cookies. By using this site you agree to the use of cookies, our policies, copyright terms and
other conditions. Read our Privacy Policy
https://www.techiedelight.com/find-execution-time-c-program/ 8/8