Happy new year
[yaz-moved-to-github.git] / src / timing.c
index 31f1095..31c9e35 100644 (file)
@@ -1,8 +1,6 @@
-/*
- * Copyright (C) 1995-2007, Index Data ApS
+/* This file is part of the YAZ toolkit.
+ * Copyright (C) 1995-2009 Index Data
  * See the file LICENSE for details.
- *
- * $Id: timing.c,v 1.2 2007-01-05 11:44:49 adam Exp $
  */
 
 /**
@@ -14,6 +12,9 @@
 #include <config.h>
 #endif
 
+#ifdef WIN32
+#include <windows.h>
+#endif
 #include <stdlib.h>
 
 #if HAVE_SYS_TIMES_H
@@ -34,16 +35,48 @@ struct yaz_timing {
 #if HAVE_SYS_TIME_H
     struct timeval start_time, end_time;
 #endif
+#ifdef WIN32
+    LONGLONG start_time, end_time;
+    LONGLONG start_time_sys, start_time_user;
+    LONGLONG end_time_sys, end_time_user;
+#endif
     double real_sec, user_sec, sys_sec;
 };
 
 yaz_timing_t yaz_timing_create(void)
 {
-    yaz_timing_t t = xmalloc(sizeof(*t));
+    yaz_timing_t t = (yaz_timing_t) xmalloc(sizeof(*t));
     yaz_timing_start(t);
     return t;
 }
 
+#ifdef WIN32
+static void get_process_time(ULONGLONG *lp_user, ULONGLONG *lp_sys)
+{
+    FILETIME create_t, exit_t, sys_t, user_t;
+    ULARGE_INTEGER li;
+
+    GetProcessTimes(GetCurrentProcess(), &create_t, &exit_t, &sys_t, &user_t);
+    li.LowPart = user_t.dwLowDateTime;
+    li.HighPart = user_t.dwHighDateTime;
+    *lp_user = li.QuadPart;
+
+    li.LowPart = sys_t.dwLowDateTime;
+    li.HighPart = sys_t.dwHighDateTime;
+    *lp_sys = li.QuadPart;
+}
+static void get_date_as_largeinteger(LONGLONG *lp)
+{
+    FILETIME f;
+    ULARGE_INTEGER li;
+    GetSystemTimeAsFileTime(&f);
+    li.LowPart = f.dwLowDateTime;
+    li.HighPart = f.dwHighDateTime;
+
+    *lp = li.QuadPart;
+}
+#endif
+
 void yaz_timing_start(yaz_timing_t t)
 {
 #if HAVE_SYS_TIMES_H
@@ -54,11 +87,17 @@ void yaz_timing_start(yaz_timing_t t)
     t->user_sec = -1.0;
     t->sys_sec = -1.0;
 #endif
+    t->real_sec = -1.0;
 #if HAVE_SYS_TIME_H
     gettimeofday(&t->start_time, 0);
     t->real_sec = 0.0;
-#else
-    t->real_sec = -1.0;
+#endif
+#ifdef WIN32
+    t->real_sec = 0.0;
+    t->user_sec = 0.0;
+    t->sys_sec = 0.0;
+    get_date_as_largeinteger(&t->start_time);
+    get_process_time(&t->start_time_user, &t->start_time_sys);
 #endif
 }
 
@@ -76,6 +115,14 @@ void yaz_timing_stop(yaz_timing_t t)
                    t->end_time.tv_usec - t->start_time.tv_usec) / 1000000;
     
 #endif
+#ifdef WIN32
+    get_date_as_largeinteger(&t->end_time);
+    t->real_sec = (t->end_time - t->start_time) / 10000000.0;
+
+    get_process_time(&t->end_time_user, &t->end_time_sys);
+    t->user_sec = (t->end_time_user - t->start_time_user) / 10000000.0;
+    t->sys_sec = (t->end_time_sys - t->start_time_sys) / 10000000.0;
+#endif
 }
 
 double yaz_timing_get_real(yaz_timing_t t)