ViSP
 All Classes Functions Variables Enumerations Enumerator Friends Groups Pages
testTime.cpp
1 /****************************************************************************
2  *
3  * $Id: testTime.cpp 4056 2013-01-05 13:04:42Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 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 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 
65 
66 // List of allowed command line options
67 #define GETOPTARGS "h"
68 
74 void usage(const char *name, const char *badparam)
75 {
76  fprintf(stdout, "\n\
77 Time management.\n\
78 \n\
79 SYNOPSIS\n\
80  %s [-h]\n", name);
81 
82  fprintf(stdout, "\n\
83 OPTIONS: Default\n\
84  -h\n\
85  Print the help.\n");
86 
87  if (badparam)
88  fprintf(stdout, "\nERROR: Bad parameter [%s]\n", badparam);
89 }
97 bool getOptions(int argc, const char **argv)
98 {
99  const char *optarg;
100  int c;
101  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
102 
103  switch (c) {
104  case 'h': usage(argv[0], NULL); return false; break;
105 
106  default:
107  usage(argv[0], optarg);
108  return false; break;
109  }
110  }
111 
112  if ((c == 1) || (c == -1)) {
113  // standalone param or error
114  usage(argv[0], NULL);
115  std::cerr << "ERROR: " << std::endl;
116  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
117  return false;
118  }
119 
120  return true;
121 }
122 
123 
124 int
125 main(int argc, const char ** argv)
126 {
127  // Read the command line options
128  if (getOptions(argc, argv) == false) {
129  exit (-1);
130  }
131 
132  double v = 0;
133 
134  double t0 = vpTime::measureTimeMs();
135  for (int i =0 ; i < 100000; i ++)
136  for (int j =0 ; j < 100; j ++)
137  v = i * 2 / 3. + j;
138  std::cout << "Computed dummy value: " << v << std::endl;
139 
140  double t1 = vpTime::measureTimeMs();
141  vpTime::wait(t1, 40);
142 
143  double t2 = vpTime::measureTimeMs();
144 
145  // Sleep 10ms
146 #if defined UNIX
147  usleep(10*1000);
148 #elif defined WIN32
149  Sleep(10);
150 #endif
151 
152  double t3 = vpTime::measureTimeMs();
153 
154  // Sleep 2ms
155 #if defined UNIX
156  usleep(2*1000);
157 #elif defined WIN32
158  Sleep(2);
159 #endif
160  double t4 = vpTime::measureTimeMs();
161 
162  vpTime::wait(t4, 19);
163 
164  double t5 = vpTime::measureTimeMs();
165 
166  vpTime::wait(5);
167 
168  double t6 = vpTime::measureTimeMs();
169 
170  vpTime::wait(21);
171 
172  double t7 = vpTime::measureTimeMs();
173 
174  vpTime::wait(2);
175 
176  double t8 = vpTime::measureTimeMs();
177 
178  std::cout << "t1-t0: computation time: " << t1 - t0 << std::endl;
179  std::cout << "t2-t1: wait(t1, 40 ms): " << t2 - t1 << std::endl;
180  std::cout << "t3-t2: sleep(10 ms): " << t3 - t2 << std::endl;
181  std::cout << "t4-t3: sleep(2 ms): " << t4 - t3 << std::endl;
182  std::cout << "t5-t4: wait(t, 19 ms): " << t5 - t4 << std::endl;
183  std::cout << "t6-t5: wait(5 ms): " << t6 - t5 << std::endl;
184  std::cout << "t7-t6: wait(21 ms): " << t7 - t6 << std::endl;
185  std::cout << "t8-t7: wait(2 ms): " << t8 - t7 << std::endl;
186 
187  return 0;
188 }
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:79