Visual Servoing Platform  version 3.4.0
testTime.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Time management.
33  *
34  * Authors:
35  * Fabien Spindler
36  *
37  *****************************************************************************/
38 
46 #include <visp3/core/vpConfig.h>
47 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
48 #include <unistd.h>
49 #elif defined(_WIN32)
50 //#include <mmsystem.h>
51 //#include <winbase.h>
52 #include <windows.h>
53 #endif
54 #include <cmath>
55 #include <iostream>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <time.h>
59 
60 #include <visp3/core/vpTime.h>
61 
62 int main()
63 {
64 #if !defined(WINRT)
65  try {
66  double v = 0;
67 
68  double t0 = vpTime::measureTimeMs();
69  for (int i = 0; i < 100000; i++)
70  for (int j = 0; j < 100; j++)
71  v = i * 2 / 3. + j;
72  std::cout << "Computed dummy value: " << v << std::endl;
73 
74  double t1 = vpTime::measureTimeMs();
75  vpTime::wait(t1, 40);
76 
77  double t2 = vpTime::measureTimeMs();
78 
79 // Sleep 10ms
80 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
81  usleep(10 * 1000);
82 #elif defined(_WIN32)
83  Sleep(10);
84 #endif
85 
86  double t3 = vpTime::measureTimeMs();
87 
88 // Sleep 2ms
89 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
90  usleep(2 * 1000);
91 #elif defined(_WIN32)
92  Sleep(2);
93 #endif
94  double t4 = vpTime::measureTimeMs();
95 
96  vpTime::wait(t4, 19);
97 
98  double t5 = vpTime::measureTimeMs();
99 
100  vpTime::wait(5);
101 
102  vpChrono chrono;
103  chrono.start();
104  double t6 = vpTime::measureTimeMs();
105 
106  vpTime::wait(21);
107 
108  chrono.stop();
109  chrono.start(false);
110  double t7 = vpTime::measureTimeMs();
111 
112  vpTime::wait(2);
113 
114  double t8 = vpTime::measureTimeMs();
115  chrono.stop();
116 
117  std::cout << "t1-t0: computation time: " << t1 - t0 << std::endl;
118 
119  std::cout << "t2-t1: wait(t1, 40 ms): " << t2 - t1 << std::endl;
120  std::cout << "t3-t2: sleep(10 ms): " << t3 - t2 << std::endl;
121  std::cout << "t4-t3: sleep(2 ms): " << t4 - t3 << std::endl;
122  std::cout << "t5-t4: wait(t, 19 ms): " << t5 - t4 << std::endl;
123  std::cout << "t6-t5: wait(5 ms): " << t6 - t5 << std::endl;
124  std::cout << "t7-t6: wait(21 ms): " << t7 - t6 << std::endl;
125  std::cout << "t8-t7: wait(2 ms): " << t8 - t7 << std::endl;
126  std::cout << "t8-t6: ; chrono: " << chrono.getDurationMs() << std::endl;
127 
128  return 0;
129  } catch (const vpException &e) {
130  std::cout << "Catch an exception: " << e << std::endl;
131  return 1;
132  }
133 #else
134  std::cout << "vpTime is not implemented on Universal Windows Platform" << std::endl;
135 #endif
136 }
VISP_EXPORT int wait(double t0, double t)
Definition: vpTime.cpp:173
double getDurationMs()
Definition: vpTime.cpp:392
error that can be emited by ViSP classes.
Definition: vpException.h:71
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:126
void start(bool reset=true)
Definition: vpTime.cpp:409
void stop()
Definition: vpTime.cpp:424