1 /* Copyright (c) 2007-2008 Massachusetts Institute of Technology
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:
11 * The above copyright notice and this permission notice shall be
12 * included in all copies or substantial portions of the Software.
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.
23 #include "nlopt-util.h"
26 #if TIME_WITH_SYS_TIME
27 # include <sys/time.h>
31 # include <sys/time.h>
37 #if defined(_WIN32) || defined(__WIN32__)
41 /* return time in seconds since some arbitrary point in the past */
42 double nlopt_seconds(void)
44 static int start_inited = 0; /* whether start time has been initialized */
45 #if defined(HAVE_GETTIMEOFDAY)
46 static struct timeval start;
50 gettimeofday(&start, NULL);
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)
56 #elif defined(_WIN32) || defined(__WIN32__)
57 static ULONGLONG start;
61 GetSystemTimeAsFileTime(&ft);
62 start = (((ULONGLONG) ft.dwHighDateTime) << 32) + ft.dwLowDateTime;
64 GetSystemTimeAsFileTime(&ft);
65 return 100e-9 * (((((ULONGLONG) ft.dwHighDateTime) << 32) + ft.dwLowDateTime) - start);
67 /* use clock() as a fallback... this is somewhat annoying
68 because clock() may wrap around with a fairly short period */
74 return (clock() - start) * 1.0 / CLOCKS_PER_SEC;
78 /* number based on time for use as random seed */
79 unsigned long nlopt_time_seed(void)
81 #if defined(HAVE_GETTIMEOFDAY)
83 gettimeofday(&tv, NULL);
84 return (tv.tv_sec ^ tv.tv_usec);
85 #elif defined(HAVE_TIME)
87 #elif defined(_WIN32) || defined(__WIN32__)
89 GetSystemTimeAsFileTime(&ft);
90 return ft.dwHighDateTime ^ ft.dwLowDateTime;