ViSP  3.0.0
testTime.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 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 <iostream>
56 #include <time.h>
57 #include <visp3/core/vpTime.h>
58 
59 int main()
60 {
61  try {
62  double v = 0;
63 
64  double t0 = vpTime::measureTimeMs();
65  for (int i =0 ; i < 100000; i ++)
66  for (int j =0 ; j < 100; j ++)
67  v = i * 2 / 3. + j;
68  std::cout << "Computed dummy value: " << v << std::endl;
69 
70  double t1 = vpTime::measureTimeMs();
71  vpTime::wait(t1, 40);
72 
73  double t2 = vpTime::measureTimeMs();
74 
75  // Sleep 10ms
76 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
77  usleep(10*1000);
78 #elif defined(_WIN32)
79  Sleep(10);
80 #endif
81 
82  double t3 = vpTime::measureTimeMs();
83 
84  // Sleep 2ms
85 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
86  usleep(2*1000);
87 #elif defined(_WIN32)
88  Sleep(2);
89 #endif
90  double t4 = vpTime::measureTimeMs();
91 
92  vpTime::wait(t4, 19);
93 
94  double t5 = vpTime::measureTimeMs();
95 
96  vpTime::wait(5);
97 
98  double t6 = vpTime::measureTimeMs();
99 
100  vpTime::wait(21);
101 
102  double t7 = vpTime::measureTimeMs();
103 
104  vpTime::wait(2);
105 
106  double t8 = vpTime::measureTimeMs();
107 
108  std::cout << "t1-t0: computation time: " << t1 - t0 << std::endl;
109  std::cout << "t2-t1: wait(t1, 40 ms): " << t2 - t1 << std::endl;
110  std::cout << "t3-t2: sleep(10 ms): " << t3 - t2 << std::endl;
111  std::cout << "t4-t3: sleep(2 ms): " << t4 - t3 << std::endl;
112  std::cout << "t5-t4: wait(t, 19 ms): " << t5 - t4 << std::endl;
113  std::cout << "t6-t5: wait(5 ms): " << t6 - t5 << std::endl;
114  std::cout << "t7-t6: wait(21 ms): " << t7 - t6 << std::endl;
115  std::cout << "t8-t7: wait(2 ms): " << t8 - t7 << std::endl;
116 
117  return 0;
118  }
119  catch(vpException e) {
120  std::cout << "Catch an exception: " << e << std::endl;
121  return 1;
122  }
123 }
VISP_EXPORT int wait(double t0, double t)
Definition: vpTime.cpp:150
error that can be emited by ViSP classes.
Definition: vpException.h:73
VISP_EXPORT double measureTimeMs()
Definition: vpTime.cpp:93