Timer.cxx 1.84 KB
Newer Older
1
/*
2
  Copyright (c) 2006 - 2018
3
  CLST  - Radboud University
4
  ILK   - Tilburg University
5

6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
  This file is part of ticcutils

  ticcutils is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 3 of the License, or
  (at your option) any later version.

  ticcutils 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 Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, see <http://www.gnu.org/licenses/>.

  For questions and suggestions, see:
22
      https://github.com/LanguageMachines/ticcutils/issues
23
  or send mail to:
24
      lamasoftware (at ) science.ru.nl
25 26
*/

27 28
#include <ticcutils/Timer.h>

29 30 31 32 33 34 35 36 37 38 39
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

namespace TiCC {

  void Timer::stop(){
    timeval now;
    gettimeofday( &now, 0 );
40
    long usecs = (myTime.tv_sec + now.tv_sec - startTime.tv_sec) * 1000000
41 42 43 44 45 46 47 48 49
      + myTime.tv_usec + now.tv_usec - startTime.tv_usec;
    ldiv_t div = ldiv( usecs, 1000000 );
    myTime.tv_sec = div.quot;
    myTime.tv_usec = div.rem;
  }

  string Timer::now() {
    time_t now;
    time( &now );
50 51 52 53
    //return asctime( localtime( &now ) );
    char buffer[256];
    strftime( buffer, 100, "%c", localtime( &now ) );
    return buffer;
54 55 56 57 58 59 60 61
  }

  ostream& operator << ( ostream& os, const Timer& T ){
    ldiv_t div = ldiv( T.myTime.tv_usec, 1000 );
    os << T.myTime.tv_sec << " seconds, " << div.quot << " milliseconds and "
       << div.rem << " microseconds";
    return os;
  }
62

63 64 65 66 67 68 69
  string Timer::toString(){
    stringstream os;
    os << *this;
    return os.str();
  }

}