Skip to content

Instantly share code, notes, and snippets.

@kosh04
Created August 28, 2014 07:33
Show Gist options
  • Save kosh04/b00f3cbd06c27aa631b9 to your computer and use it in GitHub Desktop.
Save kosh04/b00f3cbd06c27aa631b9 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdint.h>
#include <time.h>
#include <unistd.h>
#include <sys/time.h>
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#define sleep(sec) Sleep(sec * 1000)
#endif
// http://www.binarytides.com/get-time-difference-in-microtime-in-c
double time_diff(struct timeval x, struct timeval y)
{
double x_ms, y_ms;
x_ms = (double)x.tv_sec * 1000000 + (double)x.tv_usec;
y_ms = (double)y.tv_sec * 1000000 + (double)y.tv_usec;
return y_ms - x_ms;
}
// $newlisp/nl-filesys.c
// returns a differerence of 2 timeval structs in microseconds
uint64_t timediff64_us(struct timeval x, struct timeval y)
{
uint64_t usec;
if((x.tv_usec -= y.tv_usec) < 0) {
x.tv_sec--;
x.tv_usec += 1000000;
}
x.tv_sec -= y.tv_sec;
usec = 1000000 * x.tv_sec + x.tv_usec;
return usec;
}
int main()
{
struct timeval start, end;
gettimeofday(&start, NULL);
sleep(5);
gettimeofday(&end, NULL);
printf("Total time elapsed\n");
printf("time_diff : %6.0f us\n", time_diff(start, end));
printf("timediff64_us : %6.0f us\n", (double)timediff64_us(end, start));
return 0;
}
@kosh04
Copy link
Author

kosh04 commented Aug 28, 2014

[Wandbox]三へ( へ՞ਊ ՞)へ ハッハッ http://melpon.org/wandbox/permlink/S6CsftkhoQE0tjLo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment