STIR 6.4.0
CPUTimer.inl
Go to the documentation of this file.
1//
2//
15/*
16 Copyright (C) 2000 PARAPET partners
17 Copyright (C) 2000- 2009, Hammersmith Imanet Ltd
18 This file is part of STIR.
19
20 SPDX-License-Identifier: Apache-2.0 AND License-ref-PARAPET-license
21
22 See STIR/LICENSE.txt for details
23*/
24
25/*
26 History:
27 1.0 by Kris Thielemans
28 1.1 by Kris Thielemans
29 use times() and GetProcessTimes()
30 1.2 by Kris Thielemans
31 moved inlines to separate file
32*/
33
34#if defined(__OS_UNIX__) && !defined(STIR_CPUTimer_use_clock)
35// use times() instead of clock() for Unix. (Higher resolution)
36// Only tested for AIX, sun, OSF, but it is probably POSIX
37// If does not work for your OS, use the version with clock() below
38
39# include <sys/times.h>
40# include <unistd.h>
41
42START_NAMESPACE_STIR
43
44double
45CPUTimer::get_current_value() const
46{
47 struct tms t;
48 times(&t);
49 return double(t.tms_utime) / sysconf(_SC_CLK_TCK);
50}
51
52#elif defined(__OS_WIN__) && !defined(STIR_CPUTimer_use_clock)
53
54// VC++ 5.0 needs GetProcessTimes(), due to a bug in clock()
55// This breaks on Win95 though
56# include <windows.h>
57
58START_NAMESPACE_STIR
59
60// undefine the min,max macros again (we did this already in Tomography_common.h)
61# ifdef max
62# undef max
63# endif
64# ifdef min
65# undef min
66# endif
67
68double
69CPUTimer::get_current_value() const
70{
71 FILETIME CreationTime; // when the process was created
72 FILETIME ExitTime; // when the process exited
73 FILETIME KernelTime; // time the process has spent in kernel mode
74 FILETIME UserTime; // time the process has spent in user mode
75
76 GetProcessTimes(GetCurrentProcess(), &CreationTime, &ExitTime, &KernelTime, &UserTime);
77 // gcc still has problems with LARGE_INTEGER
78# ifndef __GNUG__
79 LARGE_INTEGER ll;
80 ll.LowPart = UserTime.dwLowDateTime;
81 ll.HighPart = UserTime.dwHighDateTime;
82
83 return static_cast<double>(ll.QuadPart) / 1E7;
84# else
85 // can't use LONGLONG in cygwin B20.1
86 const long long value = (static_cast<long long>(UserTime.dwHighDateTime) << 32) + UserTime.dwLowDateTime;
87 return static_cast<double>(value) / 1E7;
88# endif
89}
90
91#else // all other systems
92
93# include <time.h>
94
95START_NAMESPACE_STIR
96
97double
98CPUTimer::get_current_value() const
99{
100 return double(clock()) / CLOCKS_PER_SEC;
101}
102#endif
103
104END_NAMESPACE_STIR