chiark / gitweb /
copyright updates
[nlopt.git] / util / timer.c
1 /* Copyright (c) 2007-2008 Massachusetts Institute of Technology
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining
4  * a copy of this software and associated documentation files (the
5  * "Software"), to deal in the Software without restriction, including
6  * without limitation the rights to use, copy, modify, merge, publish,
7  * distribute, sublicense, and/or sell copies of the Software, and to
8  * permit persons to whom the Software is furnished to do so, subject to
9  * the following conditions:
10  * 
11  * The above copyright notice and this permission notice shall be
12  * included in all copies or substantial portions of the Software.
13  * 
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
21  */
22
23 #include "nlopt-util.h"
24 #include "config.h"
25
26 #if TIME_WITH_SYS_TIME
27 # include <sys/time.h>
28 # include <time.h>
29 #else
30 # if HAVE_SYS_TIME_H
31 #  include <sys/time.h>
32 # else
33 #  include <time.h>
34 # endif
35 #endif
36
37 #if defined(_WIN32) || defined(__WIN32__)
38 #  include <windows.h>    
39 #endif
40
41 /* return time in seconds since some arbitrary point in the past */
42 double nlopt_seconds(void)
43 {
44      static int start_inited = 0; /* whether start time has been initialized */
45 #if defined(HAVE_GETTIMEOFDAY)
46      static struct timeval start;
47      struct timeval tv;
48      if (!start_inited) {
49           start_inited = 1;
50           gettimeofday(&start, NULL);
51      }
52      gettimeofday(&tv, NULL);
53      return (tv.tv_sec - start.tv_sec) + 1.e-6 * (tv.tv_usec - start.tv_sec);
54 #elif defined(HAVE_TIME)
55      return time(NULL);
56 #elif defined(_WIN32) || defined(__WIN32__)
57      static ULONGLONG start;
58      FILETIME ft;
59      if (!start_inited) {
60           start_inited = 1;
61           GetSystemTimeAsFileTime(&ft);
62           start = (((ULONGLONG) ft.dwHighDateTime) << 32) + ft.dwLowDateTime;
63      }
64      GetSystemTimeAsFileTime(&ft);
65      return 100e-9 * (((((ULONGLONG) ft.dwHighDateTime) << 32) + ft.dwLowDateTime) - start);
66 #else
67      /* use clock() as a fallback... this is somewhat annoying
68         because clock() may wrap around with a fairly short period */
69      static clock_t start;
70      if (!start_inited) {
71           start_inited = 1;
72           start = clock();
73      }
74      return (clock() - start) * 1.0 / CLOCKS_PER_SEC;
75 #endif
76 }
77
78 /* number based on time for use as random seed */
79 unsigned long nlopt_time_seed(void)
80 {
81 #if defined(HAVE_GETTIMEOFDAY)
82      struct timeval tv;
83      gettimeofday(&tv, NULL);
84      return (tv.tv_sec ^ tv.tv_usec);
85 #elif defined(HAVE_TIME)
86      return time(NULL);
87 #elif defined(_WIN32) || defined(__WIN32__)
88      FILETIME ft;
89      GetSystemTimeAsFileTime(&ft);
90      return ft.dwHighDateTime ^ ft.dwLowDateTime;
91 #else
92      return clock();
93 #endif
94 }