Point Cloud Library (PCL)  1.14.0-dev
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 #pragma once
37 
38 #include <iostream>
39 #include <cmath>
40 #include <string>
41 
42 #ifdef _WIN32
43 # define NOMINMAX
44 # define WIN32_LEAN_AND_MEAN
45 # include <Windows.h>
46 # include <time.h>
47 #else
48 # include <sys/time.h>
49 #endif
50 
51 /////////////////////////////////////////////////////////////////////
52 // Note: this file is here because NVCC can't deal with all boost
53 // things, e.g. the new pcl::ScopeTime implementation
54 /////////////////////////////////////////////////////////////////////
55 
56 namespace pcl
57 {
58  namespace cuda
59  {
60  /**
61  * \brief Class to measure the time spent in a scope
62  *
63  * To use this class, e.g. to measure the time spent in a function,
64  * just create an instance at the beginning of the function.
65  * \ingroup common
66  */
68  {
69  public:
70  inline ScopeTimeCPU (const char* title);
71  inline ~ScopeTimeCPU ();
72  private:
73  std::string title_;
74  double start_time_;
75  };
76 
77 
78 #ifndef MEASURE_FUNCTION_TIME
79 #define MEASURE_FUNCTION_TIME \
80  ScopeTimeCPU scopeTime(__func__)
81 #endif
82 
83  inline double getTime ();
84 
85 /// Executes code, only if secs are gone since last exec.
86 #ifndef DO_EVERY_TS
87 #define DO_EVERY_TS(secs, currentTime, code) \
88 if (1) {\
89  static double s_lastDone_ = 0.0; \
90  double s_now_ = (currentTime); \
91  if (s_lastDone_ > s_now_) \
92  s_lastDone_ = s_now_; \
93  if (s_now_ - s_lastDone_ > (secs)) { \
94  code; \
95  s_lastDone_ = s_now_; \
96  }\
97 } else \
98  (void)0
99 #endif
100 
101 /// Executes code, only if secs are gone since last exec.
102 #ifndef DO_EVERY
103 #define DO_EVERY(secs, code) \
104  DO_EVERY_TS(secs, pcl::cuda::getTime(), code)
105 #endif
106 
107  } // end namespace
108 } // end namespace
109 
110 
111 inline double
113 {
114 #ifdef _WIN32
115  LARGE_INTEGER frequency;
116  LARGE_INTEGER timer_tick;
117  QueryPerformanceFrequency(&frequency);
118  QueryPerformanceCounter(&timer_tick);
119  return (double)(timer_tick.QuadPart)/(double)frequency.QuadPart;
120 #else
121  timeval current_time;
122  gettimeofday (&current_time, nullptr);
123  return (current_time.tv_sec + 1e-6 * current_time.tv_usec);
124 #endif
125 }
126 
127 inline pcl::cuda::ScopeTimeCPU::ScopeTimeCPU (const char* title) : title_ (title)
128 {
129  start_time_ = pcl::cuda::getTime ();
130 }
131 
133 {
134  double end_time = pcl::cuda::getTime ();
135  double duration = end_time - start_time_;
136  std::cerr << title_ << " took " << 1000 * duration << "ms. " << std::endl;
137 }
Class to measure the time spent in a scope.
Definition: time_cpu.h:68
ScopeTimeCPU(const char *title)
Definition: time_cpu.h:127
double getTime()
Definition: time_cpu.h:112