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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.utils.datastructures;
\r
14 import java.io.PrintStream;
\r
15 import java.util.concurrent.TimeUnit;
\r
19 * @author Tuukka Lehtonen
\r
21 public final class TimingUtil {
\r
23 public abstract static class NamedRunnable implements Runnable {
\r
25 public NamedRunnable(String name) {
\r
29 public String toString() {
\r
34 public static class PrintCallback implements BinaryCallback<Long, TimeUnit> {
\r
37 PrintCallback(String task) {
\r
38 this(task, System.out);
\r
40 PrintCallback(String task, PrintStream stream) {
\r
42 this.stream = stream;
\r
45 public void run(Long arg1, TimeUnit arg2) {
\r
46 stream.println(task + " took " + arg1 + " " + arg2.toString());
\r
50 public static void time(Runnable runnable) {
\r
51 time(runnable, System.out);
\r
54 public static void time(Runnable runnable, PrintStream stream) {
\r
55 time(runnable, TimeUnit.MILLISECONDS, new PrintCallback(runnable.toString(), stream));
\r
58 public static void time(Runnable runnable, TimeUnit reportTimeUnit, PrintStream stream) {
\r
59 time(runnable, reportTimeUnit, new PrintCallback(runnable.toString(), stream));
\r
62 public static void time(Runnable runnable, TimeUnit reportTimeUnit, BinaryCallback<Long, TimeUnit> result) {
\r
63 long start = System.nanoTime();
\r
65 long end = System.nanoTime();
\r
66 result.run(reportTimeUnit.convert(end-start, TimeUnit.NANOSECONDS), reportTimeUnit);
\r