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