Squashed 'subtree/rabit/' changes from 7568f75..f52daf9

f52daf9 make timer cross platform

git-subtree-dir: subtree/rabit
git-subtree-split: f52daf9be158c036c6d59058335f417cb91c82cd
This commit is contained in:
tqchen 2015-04-19 00:05:15 -07:00
parent 6bc5d6f0b4
commit 20da8bbe50

View File

@ -18,7 +18,8 @@ namespace utils {
* \brief return time in seconds, not cross platform, avoid to use this in most places
*/
inline double GetTime(void) {
#ifdef __MACH__
// TODO: use c++11 chrono when c++11 was available
#ifdef __MACH__
clock_serv_t cclock;
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
@ -26,9 +27,14 @@ inline double GetTime(void) {
mach_port_deallocate(mach_task_self(), cclock);
return static_cast<double>(mts.tv_sec) + static_cast<double>(mts.tv_nsec) * 1e-9;
#else
#if defined(__unix__) || defined(__linux__)
timespec ts;
utils::Check(clock_gettime(CLOCK_REALTIME, &ts) == 0, "failed to get time");
return static_cast<double>(ts.tv_sec) + static_cast<double>(ts.tv_nsec) * 1e-9;
#else
// TODO: add MSVC macro, and MSVC timer
return static_cast<double>(time(NULL));
#endif
#endif
}
} // namespace utils