Mercurial > projects > dynamin
annotate dynamin/core/benchmark.d @ 103:73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
author | Jordan Miner <jminer7@gmail.com> |
---|---|
date | Tue, 15 May 2012 22:06:02 -0500 |
parents | aa4efef0f0b1 |
children | 5c8c1c2e12c0 |
rev | line source |
---|---|
0 | 1 |
2 /* | |
103
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
3 * Copyright Jordan Miner |
0 | 4 * |
103
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
5 * Distributed under the Boost Software License, Version 1.0. |
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
6 * (See accompanying file BOOST_LICENSE.txt or copy at |
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
7 * http://www.boost.org/LICENSE_1_0.txt) |
0 | 8 * |
9 */ | |
10 | |
11 module dynamin.core.benchmark; | |
12 | |
13 import dynamin.all_core; | |
14 import dynamin.core.string; | |
15 import tango.io.Stdout; | |
16 | |
17 /** | |
18 * Returns: The average number of milliseconds one call of the specified | |
19 * delegate took. | |
20 */ | |
21 real benchmark(int repetitions, void delegate() dg) { // use static opCall()? | |
22 long time = Environment.runningTime; | |
23 for(int i = 0; i < repetitions; ++i) | |
24 dg(); | |
25 return (Environment.runningTime-time)/cast(real)repetitions; | |
26 } | |
27 real benchmark(void delegate() dg) { | |
28 return benchmark(1, dg); | |
29 } | |
30 /** | |
31 * name can be null | |
32 */ | |
33 real benchmarkAndWrite(string name, int repetitions, void delegate() dg) { | |
34 real time = benchmark(repetitions, dg); | |
35 Stdout.format("{} took {:.2}ms.", name, time).newline; // TODO: verify :.2 | |
36 return time; | |
37 } | |
38 real benchmarkAndWrite(string name, void delegate() dg) { | |
39 return benchmarkAndWrite(name, 1, dg); | |
40 } | |
41 | |
42 /** | |
43 * As the constructor calls the Start() method, the only time one would need | |
44 * to is when reusing a Benchmark object. | |
45 */ | |
46 class Benchmark { | |
47 long _startTime; | |
48 this() { | |
49 start(); | |
50 } | |
51 void start() { | |
52 _startTime = Environment.runningTime; | |
53 } | |
54 long time() { | |
55 return _startTime-Environment.runningTime; | |
56 } | |
57 void writeTime(string opName) { | |
58 if(opName is null) | |
59 opName = "Benchmark"; | |
60 Stdout.format("{} took {}ms.", opName, time).newline; | |
61 } | |
62 | |
63 /** | |
64 * calls the specified delegate the specified number of times and | |
65 * returns the average time one call took | |
66 */ | |
67 static long measure(int times, void delegate() d) { | |
68 long time = Environment.runningTime; | |
69 for(int i = 0; i < times; ++i) | |
70 d(); | |
71 return (Environment.runningTime-time)/times; | |
72 } | |
73 } |