21/09/2019 How to find the execution time of a C program - Techie Delight
Techie Delight
Coding made easy
All Problems Array Tree Linked List DP Graph Backtracking Matrix
Heap D&C String Sorting Stack Queue Binary Puzzles IDE
How to nd the execution Custom Search
time of a C program
In this post, we will discuss how to nd
the execution time of a C program in
windows and linux environment.
Cheap Domain Names
There are four commonly used methods to
Get Your Domain & Launch Your
nd the execution time of a C program –
Website Today. Free WhoisGuard
Privacy Protection Included
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
stores the number of clock ticks. Microsoft Must Know Priority
In order to compute the number of seconds
Queue Probability Recursive
elapsed, we need to divide the number of Searching Sliding Window
clock ticks elapsed by CLOCKS_PER_SEC Tabulation Tricky Trie
macro (also present in <time.h>) as shown
This website uses cookies. By using this site you agree to the use of cookies, our policies, copyright terms and
below:
other conditions. Read our Privacy Policy
Close and accept
[Link] 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(
// main function to find the executio
int main()
{
// to store execution time of cod
double time_spent = 0.0;
clock_t begin = clock();
// do some stuff here
sleep(3);
clock_t end = clock();
// calculate elapsed time by find
// dividing the difference by CLO
time_spent += (double)(end - begi
printf("Time elpased is %f second
return 0;
}
Download
Run Code
Output (may vary):
Subscribe to posts
Time elpased is 0.000014 seconds
Enter your email address to subscribe to new
posts and receive noti cations of new posts
by email.
Please note that clock() function doesn’t
return the actual amount of time elapsed but Email Address
returns the amount of time taken by the
underlying operating system to run the Subscribe
process. In other words, the actual wall clock
time might actually be much greater.
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
[Link] 2/8
21/09/2019 How to find the execution time of a C program - Techie Delight
elapsed since the Epoch ([Link] UTC,
January 1, 1970). It takes pointer to time_t
as an argument which is usually passed as
NULL and returns time_t type. If the
argument is not NULL, then the return value
is also stored in the memory pointed by the
argument.
It’s usage is similar to clock() function as
shown below:
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 :
Time elpased is 3 seconds
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
[Link] 3/8
21/09/2019 How to find the execution time of a C program - Techie Delight
store it in the timeval structure, expressed
as seconds and microseconds.
It is de ned in <sys/time.h> header le and
takes two arguments – the rst arugment is
reference to the timeval structure and the
second argument is a null pointer. The
timeval structure is declared as below by
the <time.h> header:
struct timeval {
long tv_sec; /* seconds */
long tv_usec; /* microseconds */
};
Below code demonstates the usage of
gettimeofday() by measuring the wall clock
time:
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
[Link] 4/8
21/09/2019 How to find the execution time of a C program - Techie Delight
Output (may vary):
Time elpased is 5 seconds and 5000147
micros
This function is supported by GCC compilers
and might not work on Windows.
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 */
};
Below code calculates elapsed time using
system-wide realtime clock, identi ed by
CLOCK_REALTIME whose time represents
seconds and nanoseconds since the Epoch.
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,
[Link] 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:
Measure elapsed time
of a C++ program using
chrono library
In this post, we will discuss how to
measure elapsed time of a C++
program in seconds, milliseconds,
microseconds and nanoseconds using
chrono library. Since C++11, the best
way to measure elapsed time in C++ is
by using the chrono library which deal
(4 votes, average: 5.00 out of 5)
Thanks for cookies.
[Link] using this site you agree to the use of cookies, our policies, copyright terms and
This website uses
other conditions. Read our Privacy Policy
Please use our online compiler to post code
in comments. To contribute, get in touch Close and accept
[Link] 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:
Tweet Share 0 Share More
Misc
Find minimum and Sort a String in Java
maximum element
in an array in Java
Leave a Reply
b i link b-quote u ul ol li
code spoiler
Join the discussion (Please
use an online compiler to
post code in comments)
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
Close and accept
[Link] 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
It was done for a
Author reason. But you can
copy, download
code and even run
it online using ‘Run
code’ option.
0 Reply
6 months ago
Techie Delight © 2019 All Rights Reserved. Privacy Policy Contact us
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
Close and accept
[Link] 8/8