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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
%% -*- erlang-indent-level: 2 -*-
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2002-2009. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%%
%% %CopyrightEnd%
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Copyright (c) 2001 by Erik Johansson. All Rights Reserved
%% Time-stamp: <2008-04-20 14:53:36 richard>
%% ====================================================================
%% Module : hipe_timer
%% Purpose :
%% Notes :
%% History : * 2001-03-15 Erik Johansson (happi@it.uu.se): Created.
%% ====================================================================
%% Exports :
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-module(hipe_timer).
-export([tr/1, t/1, timer/1, time/1, empty_time/0]).
-export([advanced/2]).
t(F) ->
{EWT,ERT} = empty_time(),
{WT,RT} = time(F),
{WT-EWT,(RT-ERT)/1000}.
tr(F) ->
{EWT,ERT} = empty_time(),
{R,{WT,RT}} = timer(F),
{R,{WT-EWT,(RT-ERT)/1000}}.
empty_time() ->
WTA = erlang:monotonic_time(),
{A,_} = erlang:statistics(runtime),
WTB = erlang:monotonic_time(),
{B,_} = erlang:statistics(runtime),
{(WTB-WTA)/erlang:convert_time_unit(1, seconds, native),B-A}.
time(F) ->
WTA = erlang:monotonic_time(),
{A,_} = erlang:statistics(runtime),
F(),
WTB = erlang:monotonic_time(),
{B,_} = erlang:statistics(runtime),
{(WTB-WTA)/erlang:convert_time_unit(1, seconds, native),B-A}.
timer(F) ->
WTA = erlang:monotonic_time(),
{A,_} = erlang:statistics(runtime),
R = F(),
WTB = erlang:monotonic_time(),
{B,_} = erlang:statistics(runtime),
{R,{(WTB-WTA)/erlang:convert_time_unit(1, seconds, native),B-A}}.
advanced(_Fun, I) when I < 2 -> false;
advanced(Fun, Iterations) ->
R = Fun(),
Measurements = [t(Fun) || _ <- lists:seq(1, Iterations)],
{Wallclock, RunTime} = split(Measurements),
WMin = lists:min(Wallclock),
RMin = lists:min(RunTime),
WMax = lists:max(Wallclock),
RMax = lists:max(RunTime),
WMean = mean(Wallclock),
RMean = mean(RunTime),
WMedian = median(Wallclock),
RMedian = median(RunTime),
WVariance = variance(Wallclock),
RVariance = variance(RunTime),
WStddev = stddev(Wallclock),
RStddev = stddev(RunTime),
WVarCoff = 100 * WStddev / WMean,
RVarCoff = 100 * RStddev / RMean,
WSum = lists:sum(Wallclock),
RSum = lists:sum(RunTime),
[{wallclock,[{min, WMin},
{max, WMax},
{mean, WMean},
{median, WMedian},
{variance, WVariance},
{stdev, WStddev},
{varcoff, WVarCoff},
{sum, WSum},
{values, Wallclock}]},
{runtime,[{min, RMin},
{max, RMax},
{mean, RMean},
{median, RMedian},
{variance, RVariance},
{stdev, RStddev},
{varcoff, RVarCoff},
{sum, RSum},
{values, RunTime}]},
{iterations, Iterations},
{result, R}].
split(M) ->
split(M, [], []).
split([{W,R}|More], AccW, AccR) ->
split(More, [W|AccW], [R|AccR]);
split([], AccW, AccR) ->
{AccW, AccR}.
mean(L) ->
mean(L, 0, 0).
mean([V|Vs], No, Sum) ->
mean(Vs, No+1, Sum+V);
mean([], No, Sum) when No > 0 ->
Sum/No;
mean([], _No, _Sum) ->
exit(empty_list).
median(L) ->
S = length(L),
SL = lists:sort(L),
case even(S) of
true ->
(lists:nth((S div 2), SL) + lists:nth((S div 2) + 1, SL)) / 2;
false ->
lists:nth((S div 2), SL)
end.
even(S) ->
(S band 1) =:= 0.
%% diffs(L, V) ->
%% [X - V || X <- L].
square_diffs(L, V) ->
[(X - V) * (X - V) || X <- L].
variance(L) ->
Mean = mean(L),
N = length(L),
if N > 1 ->
lists:sum(square_diffs(L,Mean)) / (N-1);
true -> exit('too few values')
end.
stddev(L) ->
math:sqrt(variance(L)).
|