Created
August 28, 2014 07:33
-
-
Save kosh04/b00f3cbd06c27aa631b9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
[Wandbox]三へ( へ՞ਊ ՞)へ ハッハッ http://melpon.org/wandbox/permlink/S6CsftkhoQE0tjLo