Point Cloud Library (PCL)  1.8.1
time_cpu.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2010, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage, Inc. nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *
34  */
35 
36 #ifndef PCL_CUDA_TIME_CPU_H_
37 #define PCL_CUDA_TIME_CPU_H_
38 
39 #include <iostream>
40 #include <cmath>
41 #include <string>
42 
43 #ifdef _WIN32
44 # define NOMINMAX
45 # define WIN32_LEAN_AND_MEAN
46 # include <Windows.h>
47 # include <time.h>
48 #else
49 # include <sys/time.h>
50 #endif
51 
52 /////////////////////////////////////////////////////////////////////
53 // Note: this file is here because NVCC can't deal with all boost
54 // things, e.g. the new pcl::ScopeTime implementation
55 /////////////////////////////////////////////////////////////////////
56 
57 namespace pcl
58 {
59  namespace cuda
60  {
61  /**
62  * \brief Class to measure the time spent in a scope
63  *
64  * To use this class, e.g. to measure the time spent in a function,
65  * just create an instance at the beginning of the function.
66  * \ingroup common
67  */
69  {
70  public:
71  inline ScopeTimeCPU (const char* title);
72  inline ~ScopeTimeCPU ();
73  private:
74  std::string title_;
75  double start_time_;
76  };
77 
78 
79 #ifndef MEASURE_FUNCTION_TIME
80 #define MEASURE_FUNCTION_TIME \
81  ScopeTimeCPU scopeTime(__func__)
82 #endif
83 
84  inline double getTime ();
85 
86 /// Executes code, only if secs are gone since last exec.
87 #ifndef DO_EVERY_TS
88 #define DO_EVERY_TS(secs, currentTime, code) \
89 if (1) {\
90  static double s_lastDone_ = 0.0; \
91  double s_now_ = (currentTime); \
92  if (s_lastDone_ > s_now_) \
93  s_lastDone_ = s_now_; \
94  if (s_now_ - s_lastDone_ > (secs)) { \
95  code; \
96  s_lastDone_ = s_now_; \
97  }\
98 } else \
99  (void)0
100 #endif
101 
102 /// Executes code, only if secs are gone since last exec.
103 #ifndef DO_EVERY
104 #define DO_EVERY(secs, code) \
105  DO_EVERY_TS(secs, pcl::cuda::getTime(), code)
106 #endif
107 
108  } // end namespace
109 } // end namespace
110 
111 
112 inline double
114 {
115 #ifdef _WIN32
116  LARGE_INTEGER frequency;
117  LARGE_INTEGER timer_tick;
118  QueryPerformanceFrequency(&frequency);
119  QueryPerformanceCounter(&timer_tick);
120  return (double)(timer_tick.QuadPart)/(double)frequency.QuadPart;
121 #else
122  timeval current_time;
123  gettimeofday (&current_time, NULL);
124  return (current_time.tv_sec + 1e-6 * current_time.tv_usec);
125 #endif
126 }
127 
128 inline pcl::cuda::ScopeTimeCPU::ScopeTimeCPU (const char* title) : title_ (title)
129 {
130  start_time_ = pcl::cuda::getTime ();
131 }
132 
134 {
135  double end_time = pcl::cuda::getTime ();
136  double duration = end_time - start_time_;
137  std::cerr << title_ << " took " << 1000 * duration << "ms. " << std::endl;
138 }
139 
140 #endif //#ifndef PCL_NORMS_H_
Class to measure the time spent in a scope.
Definition: time_cpu.h:68
ScopeTimeCPU(const char *title)
Definition: time_cpu.h:128
double getTime()
Definition: time_cpu.h:113