package com.profile; import java.util.Collections; import java.util.HashMap; import java.util.Map; public class Profiler { private static final Map timers = Collections.synchronizedMap(new HashMap()); private static int idCount = 0; public static ProfilerID start(String className, String methodName) { ProfilerID id = new ProfilerID(idCount++, className, methodName); TimerClass timer = new TimerClass(); timer.start(); timers.put(id, timer); return id; } public static void end(ProfilerID id) { TimerClass timer = timers.remove(id); timer.stop(); System.out.println(id + " took " + timer.getTotalTimeMillis() + "ms"); } public static class ProfilerID { private final int id; private final String className; private final String methodName; private ProfilerID(int id, String className, String methodName) { this.id = id; this.className = className; this.methodName = methodName; } @Override public String toString() { return className + "." + methodName; } } }