-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeit.f90
102 lines (76 loc) · 2.63 KB
/
timeit.f90
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
92
93
94
95
96
97
98
99
100
101
102
!>@date 16.03.2022
!>@brief A stopwatch module used for execution time evaluation.
module timeit_module
use, intrinsic :: iso_fortran_env, only: output_unit, int16
use stdlib_kinds, only: dp
use stdlib_stats, only: mean, var
implicit none
private
integer, parameter :: STDREPS = 1000
type, public :: stopwatch
integer(int16) :: outunit
real(dp) :: t1 = 0_dp
real(dp) :: t2 = 0_dp
real(dp), allocatable :: runtimes(:)
contains
procedure, private :: Init
procedure :: Start
procedure :: Stop
procedure :: Reset
final :: Destroy
end type stopwatch
interface stopwatch
module procedure GetInstance, GetPtrInstance
end interface stopwatch
contains
function GetInstance(runs, outunit) result(self)
class(stopwatch), allocatable :: self
integer(int16), intent(in), optional :: runs
integer(int16), intent(in), optional :: outunit
allocate(self)
call self%Init(runs, outunit)
end function GetInstance
function GetPtrInstance(runs, outunit) result(self)
class(stopwatch), pointer :: self
integer(int16), intent(in), optional :: runs
integer(int16), intent(in), optional :: outunit
allocate(self)
call self%Init(runs, outunit)
end function GetPtrInstance
subroutine Init(self, runs, outunit)
class(stopwatch), intent(inout) :: self
integer(int16), intent(in), optional :: runs
integer(int16), intent(in), optional :: outunit
self%t1 = 0_dp; self%t2 = 0_dp
deallocate(self%runtimes)
if (present(runs)) then
allocate(self%runtimes(runs))
else
allocate(self%runtimes(STDREPS))
end if
self%runtimes = 0_dp
if (present(outunit)) then
self%outunit = outunit
else
self%outunit = output_unit
end if
end subroutine Init
subroutine Start(self)
class(stopwatch), intent(inout) :: self
call cpu_time(self%t1)
end subroutine
subroutine Stop(self)
class(stopwatch), intent(inout) :: self
call cpu_time(self%t2)
end subroutine
subroutine Reset(self, runs, outunit)
class(stopwatch), intent(inout) :: self
integer(int16), intent(in), optional :: runs
integer(int16), intent(in), optional :: outunit
call self%Init(runs, outunit)
end subroutine
subroutine Destroy(self)
type(stopwatch) :: self
if (allocated(self%runtimes)) deallocate(self%runtimes)
end subroutine Destroy
end module timeit_module