X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.utils.datastructures%2Fsrc%2Forg%2Fsimantics%2Futils%2Fdatastructures%2FTimingUtil.java;fp=bundles%2Forg.simantics.utils.datastructures%2Fsrc%2Forg%2Fsimantics%2Futils%2Fdatastructures%2FTimingUtil.java;h=ad9bb81296997ec84e6adaadedc482797c94b2ec;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/TimingUtil.java b/bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/TimingUtil.java new file mode 100644 index 000000000..ad9bb8129 --- /dev/null +++ b/bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/TimingUtil.java @@ -0,0 +1,69 @@ +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.utils.datastructures; + +import java.io.PrintStream; +import java.util.concurrent.TimeUnit; + + +/** + * @author Tuukka Lehtonen + */ +public final class TimingUtil { + + public abstract static class NamedRunnable implements Runnable { + String name; + public NamedRunnable(String name) { + this.name = name; + } + @Override + public String toString() { + return name; + } + } + + public static class PrintCallback implements BinaryCallback { + String task; + PrintStream stream; + PrintCallback(String task) { + this(task, System.out); + } + PrintCallback(String task, PrintStream stream) { + this.task = task; + this.stream = stream; + } + @Override + public void run(Long arg1, TimeUnit arg2) { + stream.println(task + " took " + arg1 + " " + arg2.toString()); + } + }; + + public static void time(Runnable runnable) { + time(runnable, System.out); + } + + public static void time(Runnable runnable, PrintStream stream) { + time(runnable, TimeUnit.MILLISECONDS, new PrintCallback(runnable.toString(), stream)); + } + + public static void time(Runnable runnable, TimeUnit reportTimeUnit, PrintStream stream) { + time(runnable, reportTimeUnit, new PrintCallback(runnable.toString(), stream)); + } + + public static void time(Runnable runnable, TimeUnit reportTimeUnit, BinaryCallback result) { + long start = System.nanoTime(); + runnable.run(); + long end = System.nanoTime(); + result.run(reportTimeUnit.convert(end-start, TimeUnit.NANOSECONDS), reportTimeUnit); + } + +}