All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.onetwo.common.profiling.Timeit Maven / Gradle / Ivy

There is a newer version: 4.7.2
Show newest version
package org.onetwo.common.profiling;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.onetwo.common.delegate.DelegateFactory;
import org.onetwo.common.delegate.DelegateMethod;
import org.onetwo.common.utils.func.ArgsReturnableClosure;

public class Timeit {
	
	public static final Timeit create(){
		return new Timeit();
	}
	
	public static final Timeit create(TimeLogger ouputer){
		return new Timeit(ouputer);
	}
	
	private Map times = new LinkedHashMap();

    private TimeLogger out;
    
	private Timeit() {
		this.out = new TimerOutputer();
	}
	private Timeit(TimeLogger ouputer) {
		super();
		this.out = ouputer;
	}
	public void ptime(String name, ArgsReturnableClosure block){
		out.log(Timeit.class, "timeit ["+name+"] : " + timeit(name, block));
	}
	public Timeit timeit(String name, ArgsReturnableClosure block){
		long start = System.currentTimeMillis();
		block.execute();
		long cost = System.currentTimeMillis() - start;
		times.put(name, cost);
		return this;
	}
	
	public void ptimeit(Object obj, String method){
		out.log(Timeit.class, "timeit ["+obj.getClass().getSimpleName()+"."+method+"] : " + timeit(obj, method));
	}
	
	public Timeit timeit(Object obj, String method){
		String name = obj.getClass().getSimpleName()+"."+method;
		DelegateMethod dmethod = DelegateFactory.create(obj, method);
		long start = System.currentTimeMillis();
		dmethod.invoke();
		long cost = System.currentTimeMillis() - start;
		times.put(name, cost);
		return this;
	}
	
	public void printAll(){
		for(Entry entry : times.entrySet()){
			out.log(Timeit.class, "timeit ["+entry.getKey()+" : " + entry.getValue());
		}
		this.times.clear();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy