<body><script type="text/javascript"> function setAttributeOnload(object, attribute, val) { if(window.addEventListener) { window.addEventListener('load', function(){ object[attribute] = val; }, false); } else { window.attachEvent('onload', function(){ object[attribute] = val; }); } } </script> <div id="navbar-iframe-container"></div> <script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script> <script type="text/javascript"> gapi.load("gapi.iframes:gapi.iframes.style.bubble", function() { if (gapi.iframes && gapi.iframes.getContext) { gapi.iframes.getContext().openChild({ url: 'https://www.blogger.com/navbar.g?targetBlogID\x3d6566853\x26blogName\x3d1%25+inspiration\x26publishMode\x3dPUBLISH_MODE_BLOGSPOT\x26navbarType\x3dBLUE\x26layoutType\x3dCLASSIC\x26searchRoot\x3dhttp://patke.blogspot.com/search\x26blogLocale\x3den\x26v\x3d2\x26homepageUrl\x3dhttp://patke.blogspot.com/\x26vt\x3d8220196945898414734', where: document.getElementById("navbar-iframe-container"), id: "navbar-iframe" }); } }); </script>
Archives
Subscribe


Saturday, June 20, 2009

Java Performance Timer

I don't normally post code on this blog, but I recently found myself in need of a java performance timer with millisecond accuracy and was shocked that I couldn't find one online. So here you go:

public class Timer
{
long _startTime = 0;

public Timer() {}

public void Start()
{
_startTime = System.currentTimeMillis();
}

public String Stop()
{
long elapsed = System.currentTimeMillis() - _startTime;
String formatMilli = String.format("%%0%dd", 3);
String milliseconds = String.format(formatMilli, elapsed % 1000);
elapsed = elapsed / 1000;
String format = String.format("%%0%dd", 2);
String seconds = String.format(format, elapsed % 60);
String minutes = String.format(format, (elapsed % 3600) / 60);
String hours = String.format(format, elapsed / 3600);
String time = hours + ":" + minutes + ":" + seconds + "." + milliseconds;
return time;
}
}

Permalink
Comments: Post a Comment