]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/TimingUtil.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils.datastructures / src / org / simantics / utils / datastructures / TimingUtil.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.utils.datastructures;\r
13 \r
14 import java.io.PrintStream;\r
15 import java.util.concurrent.TimeUnit;\r
16 \r
17 \r
18 /**\r
19  * @author Tuukka Lehtonen\r
20  */\r
21 public final class TimingUtil {\r
22 \r
23     public abstract static class NamedRunnable implements Runnable {\r
24         String name;\r
25         public NamedRunnable(String name) {\r
26             this.name = name;\r
27         }\r
28         @Override\r
29         public String toString() {\r
30             return name;\r
31         }\r
32     }\r
33 \r
34     public static class PrintCallback implements BinaryCallback<Long, TimeUnit> {\r
35         String task;\r
36         PrintStream stream;\r
37         PrintCallback(String task) {\r
38             this(task, System.out);\r
39         }\r
40         PrintCallback(String task, PrintStream stream) {\r
41             this.task = task;\r
42             this.stream = stream;\r
43         }\r
44         @Override\r
45         public void run(Long arg1, TimeUnit arg2) {\r
46             stream.println(task + " took " + arg1 + " " + arg2.toString());\r
47         }\r
48     };\r
49 \r
50     public static void time(Runnable runnable) {\r
51         time(runnable, System.out);\r
52     }\r
53 \r
54     public static void time(Runnable runnable, PrintStream stream) {\r
55         time(runnable, TimeUnit.MILLISECONDS, new PrintCallback(runnable.toString(), stream));\r
56     }\r
57 \r
58     public static void time(Runnable runnable, TimeUnit reportTimeUnit, PrintStream stream) {\r
59         time(runnable, reportTimeUnit, new PrintCallback(runnable.toString(), stream));\r
60     }\r
61 \r
62     public static void time(Runnable runnable, TimeUnit reportTimeUnit, BinaryCallback<Long, TimeUnit> result) {\r
63         long start = System.nanoTime();\r
64         runnable.run();\r
65         long end = System.nanoTime();\r
66         result.run(reportTimeUnit.convert(end-start, TimeUnit.NANOSECONDS), reportTimeUnit);\r
67     }\r
68 \r
69 }\r