ViSP
testTime.cpp
1 /****************************************************************************
2  *
3  * $Id: testTime.cpp 4658 2014-02-09 09:50:14Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Time management.
36  *
37  * Authors:
38  * Fabien Spindler
39  *
40  *****************************************************************************/
41 
49 #include <visp/vpConfig.h>
50 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
51 # include <unistd.h>
52 #elif defined(_WIN32)
53 # include <windows.h>
54 # include <mmsystem.h>
55 # include <winbase.h>
56 #endif
57 #include <stdlib.h>
58 #include <stdio.h>
59 #include <iostream>
60 #include <time.h>
61 #include <visp/vpTime.h>
62 #include <visp/vpParseArgv.h>
63 
64 void usage(const char *name, const char *badparam);
65 bool getOptions(int argc, const char **argv);
66 
67 // List of allowed command line options
68 #define GETOPTARGS "h"
69 
75 void usage(const char *name, const char *badparam)
76 {
77  fprintf(stdout, "\n\
78 Time management.\n\
79 \n\
80 SYNOPSIS\n\
81  %s [-h]\n", name);
82 
83  fprintf(stdout, "\n\
84 OPTIONS: Default\n\
85  -h\n\
86  Print the help.\n");
87 
88  if (badparam)
89  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
90 }
98 bool getOptions(int argc, const char **argv)
99 {
100  const char *optarg_;
101  int c;
102  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
103 
104  switch (c) {
105  case 'h': usage(argv[0], NULL); return false; break;
106 
107  default:
108  usage(argv[0], optarg_);
109  return false; break;
110  }
111  }
112 
113  if ((c == 1) || (c == -1)) {
114  // standalone param or error
115  usage(argv[0], NULL);
116  std::cerr << "ERROR: " << std::endl;
117  std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
118  return false;
119  }
120 
121  return true;
122 }
123 
124 
125 int
126 main(int argc, const char ** argv)
127 {
128  try {
129  // Read the command line options
130  if (getOptions(argc, argv) == false) {
131  exit (-1);
132  }
133 
134  double v = 0;
135 
136  double t0 = vpTime::measureTimeMs();
137  for (int i =0 ; i < 100000; i ++)
138  for (int j =0 ; j < 100; j ++)
139  v = i * 2 / 3. + j;
140  std::cout << "Computed dummy value: " << v << std::endl;
141 
142  double t1 = vpTime::measureTimeMs();
143  vpTime::wait(t1, 40);
144 
145  double t2 = vpTime::measureTimeMs();
146 
147  // Sleep 10ms
148 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
149  usleep(10*1000);
150 #elif defined(_WIN32)
151  Sleep(10);
152 #endif
153 
154  double t3 = vpTime::measureTimeMs();
155 
156  // Sleep 2ms
157 #if !defined(_WIN32) && (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__))) // UNIX
158  usleep(2*1000);
159 #elif defined(_WIN32)
160  Sleep(2);
161 #endif
162  double t4 = vpTime::measureTimeMs();
163 
164  vpTime::wait(t4, 19);
165 
166  double t5 = vpTime::measureTimeMs();
167 
168  vpTime::wait(5);
169 
170  double t6 = vpTime::measureTimeMs();
171 
172  vpTime::wait(21);
173 
174  double t7 = vpTime::measureTimeMs();
175 
176  vpTime::wait(2);
177 
178  double t8 = vpTime::measureTimeMs();
179 
180  std::cout << "t1-t0: computation time: " << t1 - t0 << std::endl;
181  std::cout << "t2-t1: wait(t1, 40 ms): " << t2 - t1 << std::endl;
182  std::cout << "t3-t2: sleep(10 ms): " << t3 - t2 << std::endl;
183  std::cout << "t4-t3: sleep(2 ms): " << t4 - t3 << std::endl;
184  std::cout << "t5-t4: wait(t, 19 ms): " << t5 - t4 << std::endl;
185  std::cout << "t6-t5: wait(5 ms): " << t6 - t5 << std::endl;
186  std::cout << "t7-t6: wait(21 ms): " << t7 - t6 << std::endl;
187  std::cout << "t8-t7: wait(2 ms): " << t8 - t7 << std::endl;
188 
189  return 0;
190  }
191  catch(vpException e) {
192  std::cout << "Catch an exception: " << e << std::endl;
193  return 1;
194  }
195 }
error that can be emited by ViSP classes.
Definition: vpException.h:76
static double measureTimeMs()
Definition: vpTime.cpp:86
static int wait(double t0, double t)
Definition: vpTime.cpp:149
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:80