SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Time.h
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 1999-2009 Soeren Sonnenburg
8  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
9  */
10 
11 #ifndef __TIME_H__
12 #define __TIME_H__
13 
14 #ifndef _WIN32
15 #include <sys/time.h>
16 #endif
17 #include <time.h>
18 
19 #include <shogun/lib/config.h>
20 
21 #include <shogun/lib/common.h>
22 #include <shogun/base/SGObject.h>
23 
24 #if defined(_MSC_VER) || defined(__MINGW32__)
25 
26 #ifndef _TIMEVAL_DEFINED /* also in winsock[2].h */
27 #define _TIMEVAL_DEFINED
28 struct timeval {
29  long tv_sec;
30  long tv_usec;
31 };
32 #endif /* _TIMEVAL_DEFINED */
33 
34 int gettimeofday(struct timeval* tp, void* tzp) {
35  DWORD t;
36  t = timeGetTime();
37  tp->tv_sec = t / 1000;
38  tp->tv_usec = t % 1000;
39  /* 0 indicates that the call succeeded. */
40  return 0;
41 }
42 #endif
43 namespace shogun
44 {
47 class CTime : public CSGObject
48 {
49  public:
54  CTime(bool start=true);
55  virtual ~CTime();
56 
62  clock_t cur_runtime(bool verbose=false);
63 
69  clock_t cur_runtime_diff(bool verbose=false);
70 
77  float64_t cur_runtime_diff_sec(bool verbose=false);
78 
84  float64_t start(bool verbose=false);
85 
92  float64_t cur_time_diff(bool verbose=false);
93 
99  float64_t time_diff_sec(bool verbose=false);
100 
105  float64_t stop(bool verbose=false);
106 
112  {
113  clock_t start_runtime = clock();
114  return ((float64_t) start_runtime)/CLOCKS_PER_SEC;
115  }
116 
122  {
123  timeval tv;
124  if (gettimeofday(&tv, NULL)==0)
125  return tv.tv_sec+(tv.tv_usec*1e-6);
126  else
127  return 0.0;
128  }
129 
131  virtual const char* get_name() const { return "Time"; }
132 
133  protected:
135  clock_t start_runtime;
136 
141 };
142 }
143 #endif

SHOGUN Machine Learning Toolbox - Documentation