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