Andere Tipps

Nicht eine direkte Antwort auf Ihre Frage, aber ich bin auch oft mit dieser Spitze meines Code Zeit und schrieb nur die folgenden einfachen Eklipse -> Surround Mit Vorlage:

long startTime = System.currentTimeMillis();
${line_selection}${cursor}
long totalTime = System.currentTimeMillis() - startTime;
System.out.println("Total time = " + totalTime);
System.out.println();

JUnit 4 bekam eine integrierte Timing--contraint Funktionalität.

@Test (timeout = X)

sollte es tun. X ist die maximale Anzahl von Millisekunden die Methode ausgeführt werden darf.

ist Stoppuhr von commons-lang, es erlaubt Ihnen auch Timer aufzuspalten.

Wenn Sie Frühling verwenden Sie bereits haben schöne Klasse namens Stoppuhr in Ihrem classpath hierfür vorzuschlagen.

Welche Art von Hilfe suchen Sie mit diesem Problem? Sie haben die Grundlagen vorhanden. Sie erhalten die verstrichene Zeit in Nanosekunden genau zu, was Auflösung die zugrunde liegende Betriebssystem / Hardware in der Lage ist.

Auch ... und ich weiß, du sagtest keine Profilometer ... aber ich habe hervorragende Erfahrung mit YourKit . Es bietet eine API, die Sie Profilierung zur Steuerung von außen nutzen können. Je nachdem, was Ihr genaues Problem ist, könnte dies eine zu Hinschauen wert sein.

Caliper

Und zusätzlich zu den Wikis auf dieser Website zu lesen, empfehle ich das Lesen:

JETM ist eine gute Bibliothek, dies zu tun. Es kann auch mit Min, maxs und mittelt bieten, plus kann informative grafische Darstellungen erzeugen.

handgefertigt ...

import static java.lang.System.nanoTime;

/**
 * For testing / debug purposes.
 * 
 * <pre>
 *  private Stopwatch stopwatch = new Stopwatch();
 *  ...
 *  public void method1() {
 *      stopwatch.reset();
 *      for (...) {
 *          ...
 *          stopwatch.start();
 *          operation1();
 *          stopwatch.stop();
 *          ...
 *      }
 *      System.out.println("operation 1 max timing is " + stopwatch.getMaxLapTime());
 *  }
 *  ...
 *  public void method2() {
 *      stopwatch.reset();
 *      for (...) {
 *          ...
 *          stopwatch.start();
 *          operation2();
 *          stopwatch.stop();
 *          ...
 *      }
 *      System.out.println("operation 2 max timing is " + stopwatch.getMaxLapTime());
 *  }
 * </pre>
 * 
 * @author Mykhaylo Adamovych
 */
public class Stopwatch {
    protected long totalTime;
    protected long maxLapTime;
    protected long minLapTime = Long.MAX_VALUE;
    protected long lapsCount;
    protected long lastThreshold;

    /**
     * Human readable time in seconds
     * 
     * @param nanoTime
     * @return time in seconds
     */
    public static final String toSeconds(long nanoTime) {
        return String.format("%.9f", nanoTime / 1000000000.0);
    }

    public long getAverageLapTime() {
        if (lapsCount == 0)
            return 0;
        return totalTime / lapsCount;
    }

    public long getMaxLapTime() {
        return maxLapTime;
    }

    public long getMinLapTime() {
        return minLapTime;
    }

    public long getTotalTime() {
        return totalTime;
    }

    /**
     * Returns last lap time, process statistic.
     */
    public long lapTime() {
        return processLapTime();
    }

    private long processLapTime() {
        if (lastThreshold == 0)
            throw new IllegalStateException("Stopwatch is stopped.");
        final long now = nanoTime();
        final long lapTime = now - lastThreshold;
        lapsCount++;
        totalTime += lapTime;
        if (lapTime > maxLapTime)
            maxLapTime = lapTime;
        if (lapTime < minLapTime)
            minLapTime = lapTime;
        lastThreshold = nanoTime();
        return lapTime;
    }

    /**
     * Resets statistic and starts.
     */
    public void reset() {
        totalTime = 0;
        maxLapTime = 0;
        minLapTime = Long.MAX_VALUE;
        lapsCount = 0;
        start();
    }

    /**
     * Starts time watching.
     */
    public void start() {
        lastThreshold = nanoTime();
    }

    /**
     * Suspends time watching, returns last lap time.
     */
    public long stop() {
        final long lapTime = processLapTime();
        lastThreshold = 0;
        return lapTime;
    }
}

Etwas Neues auf dem Markt ist JMH . Es ist unter dem Dach des OpenJDK-Projektes gemacht.

mit Java Simon (früher auf Google Code ), und es scheint genial! Ein einfacher Satz von Stoppuhren und Zähler mit wenigen Abhängigkeiten.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top