I try running this program to execute the time taken for function by the help of clock_gettime in Visual Studio 2015. I have followed the reference from here: https://www.cs.rutgers.edu/~pxk/416/notes/c-tutorials/gettime.html
#include <iostream>
#include <stdio.h> /* for printf */
#include <stdint.h> /* for uint64 definition */
#include <stdlib.h> /* for exit() definition */
#include <ctime>
#include<windows.h>
#define _POSIX_C_SOURCE 200809L
#define BILLION 1000000000L
void fun() {
Sleep(3);
}
int main()
{
struct timespec start, end;
int i;
uint64_t diff;
/* measure monotonic time */
clock_gettime(CLOCK_MONOTONIC, &start); /* mark start time */
fun();
clock_gettime(CLOCK_MONOTONIC, &end); /* mark the end time */
diff = BILLION * (end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec;
printf("elapsed time = %llu nanoseconds\n", (long long unsigned int) diff);
system("pause");
return 0;
}
I tried running in Linux, it works fine. But in Windows, VS 2015 shows error.
'CLOCK_MONOTONIC' : undeclared identifier
'clock_gettime': identifier not found
Please suggest me how to fix this error or how to find the elapsed time in Visual studio 2015. Thanks.