forked from CSUChico-CSCI440/Simple-Timing-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtiming.c
More file actions
21 lines (20 loc) · 705 Bytes
/
timing.c
File metadata and controls
21 lines (20 loc) · 705 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <time.h>
#include <unistd.h>
#include <stdio.h>
int main()
{
struct timespec start, end;
/*
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).
*/
sleep(10);
clock_gettime(CLOCK_MONOTONIC, &start);
sleep(10);//threads do work
clock_gettime(CLOCK_MONOTONIC, &end);
double time_taken;
time_taken = (end.tv_sec - start.tv_sec) * 1e9;
time_taken = (time_taken + (end.tv_nsec - start.tv_nsec)) * 1e-9;
printf("Time taken %.6f seconds\n", time_taken);
}