Visual Servoing Platform  version 3.6.1 under development (2024-05-16)
testTime.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 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 https://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 *****************************************************************************/
35 
43 #include <visp3/core/vpConfig.h>
44 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
45 #include <unistd.h>
46 #elif defined(_WIN32)
47 // #include <mmsystem.h>
48 // #include <winbase.h>
49 #include <windows.h>
50 #endif
51 #include <cmath>
52 #include <iostream>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <time.h>
56 
57 #include <visp3/core/vpTime.h>
58 
59 int main()
60 {
61 #if !defined(WINRT)
62  try {
63  double v = 0;
64 
65  double t0 = vpTime::measureTimeMs();
66  for (int i = 0; i < 100000; i++)
67  for (int j = 0; j < 100; j++)
68  v = i * 2 / 3. + j;
69  std::cout << "Computed dummy value: " << v << std::endl;
70 
71  double t1 = vpTime::measureTimeMs();
72  vpTime::wait(t1, 40);
73 
74  double t2 = vpTime::measureTimeMs();
75 
76 // Sleep 10ms
77 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
78  usleep(10 * 1000);
79 #elif defined(_WIN32)
80  Sleep(10);
81 #endif
82 
83  double t3 = vpTime::measureTimeMs();
84 
85 // Sleep 2ms
86 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
87  usleep(2 * 1000);
88 #elif defined(_WIN32)
89  Sleep(2);
90 #endif
91  double t4 = vpTime::measureTimeMs();
92 
93  vpTime::wait(t4, 19);
94 
95  double t5 = vpTime::measureTimeMs();
96 
97  vpTime::wait(5);
98 
99  vpChrono chrono;
100  chrono.start();
101  double t6 = vpTime::measureTimeMs();
102 
103  vpTime::wait(21);
104 
105  chrono.stop();
106  chrono.start(false);
107  double t7 = vpTime::measureTimeMs();
108 
109  vpTime::wait(2);
110 
111  double t8 = vpTime::measureTimeMs();
112  chrono.stop();
113 
114  std::cout << "t1-t0: computation time: " << t1 - t0 << std::endl;
115 
116  std::cout << "t2-t1: wait(t1, 40 ms): " << t2 - t1 << std::endl;
117  std::cout << "t3-t2: sleep(10 ms): " << t3 - t2 << std::endl;
118  std::cout << "t4-t3: sleep(2 ms): " << t4 - t3 << std::endl;
119  std::cout << "t5-t4: wait(t, 19 ms): " << t5 - t4 << std::endl;
120  std::cout << "t6-t5: wait(5 ms): " << t6 - t5 << std::endl;
121  std::cout << "t7-t6: wait(21 ms): " << t7 - t6 << std::endl;
122  std::cout << "t8-t7: wait(2 ms): " << t8 - t7 << std::endl;
123  std::cout << "t8-t6: ; chrono: " << chrono.getDurationMs() << std::endl;
124 
125  return EXIT_SUCCESS;
126  } catch (const vpException &e) {
127  std::cout << "Catch an exception: " << e << std::endl;
128  return EXIT_FAILURE;
129  }
130 #else
131  std::cout << "vpTime is not implemented on Universal Windows Platform" << std::endl;
132 #endif
133 }
void start(bool reset=true)
Definition: vpTime.cpp:399
void stop()
Definition: vpTime.cpp:414
double getDurationMs()
Definition: vpTime.cpp:388
error that can be emitted by ViSP classes.
Definition: vpException.h:59
VISP_EXPORT int wait(double t0, double t)
VISP_EXPORT double measureTimeMs()