benchmark.java

来自「java 的源代码」· Java 代码 · 共 73 行

JAVA
73
字号
package com.reddragon2046.base.utilities.data.util;

import java.io.PrintStream;
import java.io.Serializable;
import java.util.Date;

public class Benchmark
    implements Serializable
{

    public Benchmark(String string, int n)
    {
        cycle = 100;
        title = string;
        cycle = n;
    }

    public Benchmark(String string)
    {
        this(string, 0);
    }

    public Benchmark()
    {
        this("<untitled>", 0);
    }

    public void start()
    {
        begin = (new Date()).getTime();
    }

    public void stop()
    {
        total += (new Date()).getTime() - begin;
        if(count > 0 && cycle > 0 && count % cycle == 0)
            System.out.println(this);
        count++;
    }

    public long getMilliseconds()
    {
        return total;
    }

    public String getTitle()
    {
        return title;
    }

    public int getCount()
    {
        return count;
    }

    public String toString()
    {
        return "Benchmark( " + getTitle() + " x " + count + ": " + total + " ms )";
    }

    public void compareTo(Benchmark benchmark)
    {
        float ratio = (float)total / (float)benchmark.total;
        System.out.println("ratio of " + getTitle() + " to " + benchmark.getTitle() + " is " + ratio);
    }

    long begin;
    long total;
    String title;
    int count;
    int cycle;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?