-
Notifications
You must be signed in to change notification settings - Fork 413
/
timers.chpl
100 lines (86 loc) · 2.59 KB
/
timers.chpl
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
// Timers
/*
This primer demonstrates the use of a Timer from the ``Time`` module.
*/
//
// Use the ``Time`` standard module to bring its symbols into scope.
//
use Time;
//
// The ``quiet`` configuration constant is set to ``false`` when testing
// (via start_test) so that this test is deterministic.
//
config const quiet: bool = false;
//
// A ``Timer`` can be used like a stopwatch to time portions of code.
//
var t: Timer;
//
// To time a procedure, start the timer before calling the
// procedure and stop it afterwards. Here, we will time the ``sleep``
// procedure, also defined in the ``Time`` module.
//
t.start();
sleep(1);
t.stop();
//
// To report the time, use the ``elapsed()`` method. By default, the
// ``elapsed`` method reports time in seconds.
//
if !quiet then
writeln("A. ", t.elapsed(), " seconds");
//
// The elapsed time can also be checked in units other than
// seconds. The supported units are: ``microseconds``, ``milliseconds``,
// ``seconds``, ``minutes``, ``hours``.
//
if !quiet then
writeln("B. ", t.elapsed(TimeUnits.milliseconds), " milliseconds");
//
// The ``Timer`` can be started again to accumulate additional time.
//
t.start();
sleep(1);
t.stop();
if !quiet then
writeln("C. ", t.elapsed(TimeUnits.microseconds), " microseconds");
//
// To start the ``Timer`` over at zero, call the ``clear`` method.
//
t.clear();
writeln("D. ", t.elapsed(), " seconds");
//
// The timer can be checked while still running. This can be used to time
// multiple events. Here, the time taken by each iteration of the loop
// is saved into the ``iterationTimes`` array.
//
config const n = 5;
var iterationTimes: [1..n] real;
t.start();
for i in 1..n {
var startTime = t.elapsed(TimeUnits.microseconds);
//
// This code will be timed n times.
//
iterationTimes(i) = t.elapsed(TimeUnits.microseconds) - startTime;
}
t.stop();
//
// Finally, a lighter-weight, but less flexible, way of doing timings
// is to use the procedure: ``getCurrentTime(unit: TimeUnits): real``
//
// It returns the number of units (seconds by default) that have
// passed since midnight as a floating-point value. (Note that this
// makes it not particularly useful for timing things that might run
// across midnight). As a simple example, we can use the following
// idiom to time the number of seconds something will take:
//
const start = getCurrentTime();
sleep(1);
const elapsed = getCurrentTime() - start;
if !quiet then
writeln("E. ", elapsed, " seconds");
//
// For more information on Chapel's timing capabilities, refer to the
// :mod:`Time` section of the Standard Modules online documentation.
//