summaryrefslogtreecommitdiff
path: root/pod/modpods/Benchmark.pod
blob: bdb3f057008b7f5424a8e5f6f4f3f645d0fb791e (plain)
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
=head1 NAME

Benchmark - benchmark running times of code

timethis - run a chunk of code several times

timethese - run several chunks of code several times

timeit - run a chunk of code and see how long it goes

=head1 SYNOPSYS

    timethis ($count, "code");

    timethese($count, {
	'Name1' => '...code1...',
	'Name2' => '...code2...',
    });

    $t = timeit($count, '...other code...')
    print "$count loops of other code took:",timestr($t),"\n";

=head1 DESCRIPTION

The Benchmark module encapsulates a number of routines to help you
figure out how long it takes to execute some code.

=head2 Methods

=over 10

=item new

Returns the current time.   Example:

    use Benchmark;
    $t0 = new Benchmark;
    # ... your code here ...
    $t1 = new Benchmark;
    $td = timediff($t1, $t0);
    print "the code took:",timestr($dt),"\n";

=item debug

Enables or disable debugging by setting the C<$Benchmark::Debug> flag:

    debug Benchmark 1; 
    $t = timeit(10, ' 5 ** $Global ');
    debug Benchmark 0; 

=back

=head2 Standard Exports

The following routines will be exported into your namespace 
if you use the Benchmark module:

=over 10

=item timeit(COUNT, CODE)

Arguments: COUNT is the number of time to run the loop, and 
the second is the code to run.  CODE may be a string containing the code,
a reference to the function to run, or a reference to a hash containing 
keys which are names and values which are more CODE specs.

Side-effects: prints out noise to standard out.

Returns: a Benchmark object.  

=item timethis

=item timethese

=item timediff

=item timestr

=back

=head2 Optional Exports

The following routines will be exported into your namespace
if you specifically ask that they be imported:

=over 10

clearcache

clearallcache

disablecache

enablecache

=back

=head1 NOTES

The data is stored as a list of values from the time and times
functions: 

      ($real, $user, $system, $children_user, $children_system)

in seconds for the whole loop (not divided by the number of rounds).

The timing is done using time(3) and times(3).

Code is executed in the caller's package.

Enable debugging by:  

    $Benchmark::debug = 1;

The time of the null loop (a loop with the same
number of rounds but empty loop body) is subtracted
from the time of the real loop.

The null loop times are cached, the key being the
number of rounds. The caching can be controlled using
calls like these:

    clearcache($key); 
    clearallcache();

    disablecache(); 
    enablecache();

=head1 INHERITANCE

Benchmark inherits from no other class, except of course
for Exporter.

=head1 CAVEATS

The real time timing is done using time(2) and
the granularity is therefore only one second.

Short tests may produce negative figures because perl
can appear to take longer to execute the empty loop 
than a short test; try: 

    timethis(100,'1');

The system time of the null loop might be slightly
more than the system time of the loop with the actual
code and therefore the difference might end up being < 0.

More documentation is needed :-( especially for styles and formats.

=head1 AUTHORS

Jarkko Hietaniemi <Jarkko.Hietaniemi@hut.fi>,
Tim Bunce <Tim.Bunce@ig.co.uk>

=head1 MODIFICATION HISTORY

September 8th, 1994; by Tim Bunce.