1 /*!
2  *  Copyright (c) 2015 by Contributors
3  * \file timer.h
4  * \brief cross platform timer for timing
5  * \author Tianqi Chen
6  */
7 #ifndef DMLC_TIMER_H_
8 #define DMLC_TIMER_H_
9 
10 #include "base.h"
11 
12 #if DMLC_USE_CXX11
13 #include <chrono>
14 #endif
15 
16 #include <time.h>
17 #ifdef __MACH__
18 #include <mach/clock.h>
19 #include <mach/mach.h>
20 #endif
21 #include "./logging.h"
22 
23 namespace dmlc {
24 /*!
25  * \brief return time in seconds
26  */
GetTime(void)27 inline double GetTime(void) {
28   #if DMLC_USE_CXX11
29   return std::chrono::duration<double>(
30       std::chrono::high_resolution_clock::now().time_since_epoch()).count();
31   #elif defined __MACH__
32   clock_serv_t cclock;
33   mach_timespec_t mts;
34   host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
35   CHECK(clock_get_time(cclock, &mts) == 0) << "failed to get time";
36   mach_port_deallocate(mach_task_self(), cclock);
37   return static_cast<double>(mts.tv_sec) + static_cast<double>(mts.tv_nsec) * 1e-9;
38   #else
39   #if defined(__unix__) || defined(__linux__)
40   timespec ts;
41   CHECK(clock_gettime(CLOCK_REALTIME, &ts) == 0) << "failed to get time";
42   return static_cast<double>(ts.tv_sec) + static_cast<double>(ts.tv_nsec) * 1e-9;
43   #else
44   return static_cast<double>(time(NULL));
45   #endif
46   #endif
47 }
48 }  // namespace dmlc
49 #endif  // DMLC_TIMER_H_
50