wfxr
4/4/2017 - 2:16 PM

Java benchmark util.

Java benchmark util.

package com.zmeng.rinascimento.pascal.common.testutil;

import java.util.function.Supplier;

import static java.lang.System.currentTimeMillis;

/**
 * Created by Wenxuan-Zhang on 2017/3/20.
 * Email: zhangwenxuan@zmeng123.com
 */
public class Benchmark {
    public static <T> T measureTime(Supplier<T> task, String taskName) {
        // JVM预热
        task.get();

        System.out.printf("Task [%s] start...\n", taskName);
        final long start = currentTimeMillis();
        T result = task.get();
        System.out.printf("Task [%s] completed using %dms.\n", taskName, currentTimeMillis() - start);

        return result;
    }
}