1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
/*
*
* Copyright (C) 1999-2012, OFFIS e.V.
* All rights reserved. See COPYRIGHT file for details.
*
* This software and supporting documentation were developed by
*
* OFFIS e.V.
* R&D Division Health
* Escherweg 2
* D-26121 Oldenburg, Germany
*
*
* Module: ofstd
*
* Author: Joerg Riesmeier
*
* Purpose: Class for measurement of time (Source)
*
*/
#include "dcmtk/config/osconfig.h"
#include "dcmtk/ofstd/oftimer.h"
#include "dcmtk/ofstd/ofcast.h"
#ifdef HAVE_WINDOWS_H
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#else /* UNIX */
#include <sys/time.h>
#endif
/*------------------*
* implementation *
*------------------*/
OFTimer::OFTimer()
: Start(getTime())
{
}
void OFTimer::reset()
{
Start = getTime();
}
double OFTimer::getDiff() const
{
return getTime() - Start;
}
double OFTimer::getDiff(double start)
{
return getTime() - start;
}
double OFTimer::getTime()
{
#ifdef HAVE_WINDOWS_H
// according to MSDN: "The resolution of the GetTickCount function is limited to the resolution
// of the system timer, which is typically in the range of 10 milliseconds to 16 milliseconds."
return OFstatic_cast(double, GetTickCount()) / 1000;
#else
timeval c_time;
gettimeofday(&c_time, NULL);
return OFstatic_cast(double, c_time.tv_sec) + OFstatic_cast(double, c_time.tv_usec) / 1000000;
#endif
}
STD_NAMESPACE ostream &operator<<(STD_NAMESPACE ostream &stream, const OFTimer &timer)
{
const double timeDiff = timer.getDiff();
// output time difference in units depending on the value range
if ((timeDiff < 1) && (timeDiff > -1))
stream << (timeDiff * 1000) << " ms";
else if ((timeDiff < 60) && (timeDiff > -60))
stream << timeDiff << " s";
else if ((timeDiff < 3600) && (timeDiff > -3600))
stream << (timeDiff / 60) << " min";
else
stream << (timeDiff / 3600) << " h";
return stream;
}
|