Clock.cpp 3.17 KB
Newer Older
Thomas Moulard's avatar
Thomas Moulard committed
1
/*
2
 * Copyright 2005, 2006, 2007, 2008, 2009, 2010,
Thomas Moulard's avatar
Thomas Moulard committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 *
 * Olivier Stasse
 *
 * JRL, CNRS/AIST
 *
 * This file is part of walkGenJrl.
 * walkGenJrl is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * walkGenJrl is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License
 * along with walkGenJrl.  If not, see <http://www.gnu.org/licenses/>.
 *
21
 *  Research carried out within the scope of the
Thomas Moulard's avatar
Thomas Moulard committed
22
23
24
25
26
 *  Joint Japanese-French Robotics Laboratory (JRL)
 */
/*! \file Clock.cpp
  \brief This object allows to make time measurement on the code.

27
28
  Copyright (c) 2008,
  Olivier Stasse,
Thomas Moulard's avatar
Thomas Moulard committed
29

30
  JRL-Japan, CNRS/AIST
Thomas Moulard's avatar
Thomas Moulard committed
31

32
33
34
  All rights reserved.

  Please see license.txt for more information on license.
Thomas Moulard's avatar
Thomas Moulard committed
35
36
*/

37
#include <Clock.hh>
38
39
#include <fstream>
#include <iostream>
Thomas Moulard's avatar
Thomas Moulard committed
40
41
42

using namespace PatternGeneratorJRL;

43
Clock::Clock() {
Thomas Moulard's avatar
Thomas Moulard committed
44
45
46
47
  Reset();
  m_DataBuffer.resize(300000);

  struct timeval startingtime;
48
  gettimeofday(&startingtime, 0);
Guilhem Saurel's avatar
Guilhem Saurel committed
49
50
  m_StartingTime =
      (double)startingtime.tv_sec + 0.000001 * (double)startingtime.tv_usec;
Thomas Moulard's avatar
Thomas Moulard committed
51
52
}

53
Clock::~Clock() {}
Thomas Moulard's avatar
Thomas Moulard committed
54

55
void Clock::Reset() {
Thomas Moulard's avatar
Thomas Moulard committed
56
57
  m_NbOfIterations = 0;
  m_MaximumTime = 0.0;
58
  m_TotalTime = 0.0;
Thomas Moulard's avatar
Thomas Moulard committed
59
60

  struct timeval startingtime;
61
  gettimeofday(&startingtime, 0);
Guilhem Saurel's avatar
Guilhem Saurel committed
62
63
  m_StartingTime =
      (double)startingtime.tv_sec + 0.000001 * (double)startingtime.tv_usec;
Thomas Moulard's avatar
Thomas Moulard committed
64
65
}

66
void Clock::StartTiming() { gettimeofday(&m_BeginTimeStamp, 0); }
Thomas Moulard's avatar
Thomas Moulard committed
67

68
69
void Clock::StopTiming() {
  gettimeofday(&m_EndTimeStamp, 0);
Guilhem Saurel's avatar
Guilhem Saurel committed
70
71
72
  double ltime =
      (double)m_EndTimeStamp.tv_sec - (double)m_BeginTimeStamp.tv_sec +
      0.000001 * (double)(m_EndTimeStamp.tv_usec - m_BeginTimeStamp.tv_usec);
Thomas Moulard's avatar
Thomas Moulard committed
73
74
75
76

  m_MaximumTime = m_MaximumTime < ltime ? ltime : m_MaximumTime;
  m_TotalTime += ltime;

77
  m_DataBuffer[(m_NbOfIterations * 2) % 3000000] =
Guilhem Saurel's avatar
Guilhem Saurel committed
78
79
      (double)m_BeginTimeStamp.tv_sec +
      0.000001 * (double)m_BeginTimeStamp.tv_usec - m_StartingTime;
80
  m_DataBuffer[(m_NbOfIterations * 2 + 1) % 3000000] = ltime;
Thomas Moulard's avatar
Thomas Moulard committed
81
82
}

83
void Clock::IncIteration(int lNbOfIts) { m_NbOfIterations += lNbOfIts; }
Thomas Moulard's avatar
Thomas Moulard committed
84

85
void Clock::RecordDataBuffer(std::string filename) {
Thomas Moulard's avatar
Thomas Moulard committed
86
  std::ofstream aof(filename.c_str());
87
88
  for (unsigned int i = 0; i < 2 * m_NbOfIterations % 300000; i += 2)
    aof << m_DataBuffer[i] << " " << m_DataBuffer[i + 1] << std::endl;
Thomas Moulard's avatar
Thomas Moulard committed
89
90
  aof.close();
}
91
unsigned long int Clock::NbOfIterations() { return m_NbOfIterations; }
Thomas Moulard's avatar
Thomas Moulard committed
92

93
double Clock::MaxTime() { return m_MaximumTime; }
Thomas Moulard's avatar
Thomas Moulard committed
94

95
double Clock::TotalTime() { return m_TotalTime; }
Thomas Moulard's avatar
Thomas Moulard committed
96

97
98
99
double Clock::AverageTime() {
  if (m_NbOfIterations != 0)
    return m_TotalTime / (double)m_NbOfIterations;
Thomas Moulard's avatar
Thomas Moulard committed
100
101
102
  return 0.0;
}

103
void Clock::Display() {
Thomas Moulard's avatar
Thomas Moulard committed
104
105
106
107
108
  std::cout << "Average Time : " << AverageTime() << std::endl;
  std::cout << "Total Time : " << TotalTime() << std::endl;
  std::cout << "Max Time : " << MaxTime() << std::endl;
  std::cout << "Nb of iterations: " << NbOfIterations() << std::endl;
}