Skip to content
Browse files

Update timing code to (maybe) be Mac compatible.

  • Loading branch information...
1 parent 9b1b00c commit 4e66723dff858f2951a885cf02ee7be9708e20d5 @inducer inducer committed Sep 8, 2012
Showing with 23 additions and 40 deletions.
  1. +5 −17 00-timing-soln.c
  2. +18 −23 timing.h
View
22 00-timing-soln.c
@@ -1,31 +1,19 @@
#include <stdio.h>
-#include <time.h>
+#include "timing.h"
-double timespec_diff_in_seconds(struct timespec start, struct timespec end)
-{
- struct timespec temp;
- if ((end.tv_nsec-start.tv_nsec)<0) {
- temp.tv_sec = end.tv_sec-start.tv_sec-1;
- temp.tv_nsec = 1000000000+end.tv_nsec-start.tv_nsec;
- } else {
- temp.tv_sec = end.tv_sec-start.tv_sec;
- temp.tv_nsec = end.tv_nsec-start.tv_nsec;
- }
- return temp.tv_sec + 1e-9*temp.tv_nsec;
-}
int main()
{
- struct timespec time1, time2;
- clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
+ timestamp_type time1, time2;
+ get_timestamp(&time1);
long int temp = 1;
for (int i = 0; i< 250000000; i++)
temp+= 3*temp-1;
- clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);
+ get_timestamp(&time2);
printf("that took %f seconds\n",
- timespec_diff_in_seconds(time1,time2));
+ timestamp_diff_in_seconds(time1,time2));
// printf("and the result was %d\n", temp);
View
41 timing.h
@@ -1,48 +1,43 @@
-#include <time.h>
+#ifdef __APPLE__
-#ifdef __APPLE___
+#include <sys/time.h>
typedef struct timeval timestamp_type;
-static void get_current_time(timestamp_type *t)
+static void get_timestamp(timestamp_type *t)
{
- gettimeofday(t);
+ gettimeofday(t, NULL);
}
-double
-timeval_subtract (timestamp_type x, timestamp_type y)
+double timestamp_diff_in_seconds(timestamp_type x, timestamp_type y)
{
/* Perform the carry for the later subtraction by updating y. */
- if (x.tv_usec < y->tv_usec) {
- int nsec = (y->tv_usec - x.tv_usec) / 1000000 + 1;
- y->tv_usec -= 1000000 * nsec;
- y->tv_sec += nsec;
+ if (x.tv_usec < y.tv_usec) {
+ int nsec = (y.tv_usec - x.tv_usec) / 1000000 + 1;
+ y.tv_usec -= 1000000 * nsec;
+ y.tv_sec += nsec;
}
- if (x.tv_usec - y->tv_usec > 1000000) {
- int nsec = (x.tv_usec - y->tv_usec) / 1000000;
- y->tv_usec += 1000000 * nsec;
- y->tv_sec -= nsec;
+ if (x.tv_usec - y.tv_usec > 1000000) {
+ int nsec = (x.tv_usec - y.tv_usec) / 1000000;
+ y.tv_usec += 1000000 * nsec;
+ y.tv_sec -= nsec;
}
- /* Compute the time remaining to wait.
- tv_usec is certainly positive. */
- result->tv_sec = x.tv_sec;
- result->tv_usec = x.tv_usec - y->tv_usec;
-
- /* Return 1 if result is negative. */
- return x.tv_sec < y->tv_sec;
+ return x.tv_sec + (x.tv_usec - y.tv_usec)*1e-6;
}
#else
+#include <time.h>
+
typedef struct timespec timestamp_type;
-static void get_current_time(timestamp_type *t)
+static void get_timestamp(timestamp_type *t)
{
clock_gettime(CLOCK_REALTIME, t);
}
-static double timespec_diff_in_seconds(timestamp_type start, timestamp_type end)
+static double timestamp_diff_in_seconds(timestamp_type start, timestamp_type end)
{
struct timespec temp;
if ((end.tv_nsec-start.tv_nsec)<0) {

0 comments on commit 4e66723

Please sign in to comment.
Something went wrong with that request. Please try again.