timer.cpp
Go to the documentation of this file.
00001 // 
00002 // Copyright (c) 2006-2012, Benjamin Kaufmann
00003 // 
00004 // This file is part of Clasp. See http://www.cs.uni-potsdam.de/clasp/ 
00005 // 
00006 // Clasp is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU General Public License as published by
00008 // the Free Software Foundation; either version 2 of the License, or
00009 // (at your option) any later version.
00010 // 
00011 // Clasp is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 // 
00016 // You should have received a copy of the GNU General Public License
00017 // along with Clasp; if not, write to the Free Software
00018 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019 //
00020 #include <clasp/util/timer.h>
00021 
00022 #ifdef _WIN32
00023 #define WIN32_LEAN_AND_MEAN // exclude APIs such as Cryptography, DDE, RPC, Shell, and Windows Sockets.
00024 #define NOMINMAX            // do not let windows.h define macros min and max
00025 #include <windows.h>        // GetProcessTimes, GetCurrentProcess, FILETIME
00026 #define TICKS_PER_SEC 10000000
00027 
00028 namespace Clasp {
00029 
00030 double RealTime::getTime() {
00031         union Convert {
00032                 FILETIME time;
00033                 __int64  asUint;
00034         } now;
00035         GetSystemTimeAsFileTime(&now.time);
00036         return static_cast<double>(now.asUint/static_cast<double>(TICKS_PER_SEC));
00037 }
00038 
00039 double ProcessTime::getTime() {
00040         FILETIME ignoreStart, ignoreExit;
00041         union Convert {
00042                 FILETIME time;
00043                 __int64  asUint;
00044         } user, system;
00045         GetProcessTimes(GetCurrentProcess(), &ignoreStart, &ignoreExit, &user.time, &system.time);
00046         return (user.asUint + system.asUint) / double(TICKS_PER_SEC);
00047 }
00048 
00049 double ThreadTime::getTime() {
00050         FILETIME ignoreStart, ignoreExit;
00051         union Convert {
00052                 FILETIME time;
00053                 __int64  asUint;
00054         } user, system;
00055         GetThreadTimes(GetCurrentThread(), &ignoreStart, &ignoreExit, &user.time, &system.time);
00056         return (user.asUint + system.asUint) / double(TICKS_PER_SEC);
00057 }
00058 
00059 }
00060 #else
00061 #include <sys/times.h>   // times()
00062 #include <sys/time.h>    // gettimeofday()
00063 #include <unistd.h>      // sysconf()                   
00064 #include <sys/resource.h>// getrusage
00065 #include <limits>
00066 namespace Clasp {
00067 
00068 double RealTime::getTime() {
00069         struct timeval now;
00070         gettimeofday(&now, NULL);
00071         return static_cast<double>(now.tv_sec) + static_cast<double>(now.tv_usec / 1000000.0);
00072 }
00073 
00074 double ProcessTime::getTime() {
00075         struct tms nowTimes;
00076         times(&nowTimes);
00077         return (nowTimes.tms_utime + nowTimes.tms_stime) / double(sysconf(_SC_CLK_TCK));
00078 }
00079 
00080 double ThreadTime::getTime() {
00081 #if defined(RUSAGE_THREAD)
00082         int who = RUSAGE_THREAD;
00083         struct rusage usage;
00084         getrusage(who, &usage);
00085         double user = static_cast<double>(usage.ru_utime.tv_sec) + static_cast<double>(usage.ru_utime.tv_usec / 1000000.0);
00086         double sys  = static_cast<double>(usage.ru_stime.tv_sec) + static_cast<double>(usage.ru_stime.tv_usec / 1000000.0);
00087         return user + sys;
00088 #else
00089         return std::numeric_limits<double>::quiet_NaN();
00090 #endif
00091 }
00092 
00093 }
00094 #endif


clasp
Author(s): Benjamin Kaufmann
autogenerated on Thu Aug 27 2015 12:41:40