Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testTime.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
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 http://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  * Authors:
34  * Fabien Spindler
35  *
36  *****************************************************************************/
37 
45 #include <visp3/core/vpConfig.h>
46 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
47 # include <unistd.h>
48 #elif defined(_WIN32)
49 # include <windows.h>
50 # include <mmsystem.h>
51 # include <winbase.h>
52 #endif
53 #include <stdlib.h>
54 #include <stdio.h>
55 #include <time.h>
56 #include <cmath>
57 #include <iostream>
58 
59 #include <visp3/core/vpTime.h>
60 
61 int main()
62 {
63 #if !defined(WINRT)
64  try {
65  double v = 0;
66 
67  double t0 = vpTime::measureTimeMs();
68  for (int i =0 ; i < 100000; i ++)
69  for (int j =0 ; j < 100; j ++)
70  v = i * 2 / 3. + j;
71  std::cout << "Computed dummy value: " << v << std::endl;
72 
73  double t1 = vpTime::measureTimeMs();
74  vpTime::wait(t1, 40);
75 
76  double t2 = vpTime::measureTimeMs();
77 
78  // Sleep 10ms
79 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
80  usleep(10*1000);
81 #elif defined(_WIN32)
82  Sleep(10);
83 #endif
84 
85  double t3 = vpTime::measureTimeMs();
86 
87  // Sleep 2ms
88 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
89  usleep(2*1000);
90 #elif defined(_WIN32)
91  Sleep(2);
92 #endif
93  double t4 = vpTime::measureTimeMs();
94 
95  vpTime::wait(t4, 19);
96 
97  double t5 = vpTime::measureTimeMs();
98 
99  vpTime::wait(5);
100 
101  double t6 = vpTime::measureTimeMs();
102 
103  vpTime::wait(21);
104 
105  double t7 = vpTime::measureTimeMs();
106 
107  vpTime::wait(2);
108 
109  double t8 = vpTime::measureTimeMs();
110 
111  std::cout << "t1-t0: computation time: " << t1 - t0 << std::endl;
112 
113  std::cout << "t2-t1: wait(t1, 40 ms): " << t2 - t1 << std::endl;
114  std::cout << "t3-t2: sleep(10 ms): " << t3 - t2 << std::endl;
115  std::cout << "t4-t3: sleep(2 ms): " << t4 - t3 << std::endl;
116  std::cout << "t5-t4: wait(t, 19 ms): " << t5 - t4 << std::endl;
117  std::cout << "t6-t5: wait(5 ms): " << t6 - t5 << std::endl;
118  std::cout << "t7-t6: wait(21 ms): " << t7 - t6 << std::endl;
119  std::cout << "t8-t7: wait(2 ms): " << t8 - t7 << std::endl;
120 
121  return 0;
122  }
123  catch(vpException &e) {
124  std::cout << "Catch an exception: " << e << std::endl;
125  return 1;
126  }
127 # else
128  std::cout << "vpTime is not implemented on Universal Windows Platform" << std::endl;
129 # endif
130 }
VISP_EXPORT int wait(double t0, double t)
Definition: vpTime.cpp:157
error that can be emited by ViSP classes.
Definition: vpException.h:73
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:93