Measuring elapsed time with the Apache StopWatch

When it comes to measuring elapsed time often the code written will be directly reliant on System.currentTimeMillis (or System.nanoTime), eg. fairly similar to this:

long startTime  = System.currentTimeMillis();
//processing here...
long endTime = System.currentTimeMillis();
long elapsedTime = endTime-startTime;

works ok … but it’s a bit verbose. Also to get the time in other units than milliseconds will involve writing additional logic to translate the elapsed time into seconds, minutes, hours…

The Apache Commons library StopWatch will save writing all of this additional code. Less code means less bugs.

Stopwatch sw=new Stopwatch();
//processing here...

2 thoughts on “Measuring elapsed time with the Apache StopWatch

  1. Johnd90

    I was looking through some of your blog posts on this internet site and I conceive this web site is rattling informative ! Keep on posting . bfddddcdcgge


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s