Mercurial > projects > dynamin
annotate dynamin/core/benchmark.d @ 106:acdbb30fee7e
Port to D2.
Most of the effort was dealing with immutable and const.
author | Jordan Miner <jminer7@gmail.com> |
---|---|
date | Mon, 17 Dec 2012 23:41:50 -0600 |
parents | 5c8c1c2e12c0 |
children |
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 */ | |
104
5c8c1c2e12c0
Change from real to double.
Jordan Miner <jminer7@gmail.com>
parents:
103
diff
changeset
|
21 double benchmark(int repetitions, void delegate() dg) { // use static opCall()? |
0 | 22 long time = Environment.runningTime; |
23 for(int i = 0; i < repetitions; ++i) | |
24 dg(); | |
104
5c8c1c2e12c0
Change from real to double.
Jordan Miner <jminer7@gmail.com>
parents:
103
diff
changeset
|
25 return (Environment.runningTime-time)/cast(double)repetitions; |
0 | 26 } |
104
5c8c1c2e12c0
Change from real to double.
Jordan Miner <jminer7@gmail.com>
parents:
103
diff
changeset
|
27 double benchmark(void delegate() dg) { |
0 | 28 return benchmark(1, dg); |
29 } | |
30 /** | |
31 * name can be null | |
32 */ | |
106 | 33 double benchmarkAndWrite(cstring name, int repetitions, void delegate() dg) { |
104
5c8c1c2e12c0
Change from real to double.
Jordan Miner <jminer7@gmail.com>
parents:
103
diff
changeset
|
34 double time = benchmark(repetitions, dg); |
0 | 35 Stdout.format("{} took {:.2}ms.", name, time).newline; // TODO: verify :.2 |
36 return time; | |
37 } | |
106 | 38 double benchmarkAndWrite(cstring name, void delegate() dg) { |
0 | 39 return benchmarkAndWrite(name, 1, dg); |
40 } | |
41 | |
42 /** | |
106 | 43 * As the constructor calls the start() method, the only time one would need |
0 | 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 } | |
106 | 57 void writeTime(cstring opName) { |
0 | 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 } |