Search code examples
javatimerstopwatch

Stopwatch class that shows min, max, average, ... times?


I am looking for a java class that simulates a StopWatch (like Spring's StopWatch or Commons' StopWatch) but that would give minimum, maximum and average times. Preferable also average over last n runs as well.

Is there such a thing or do I just build it myself?

regards,

Wim


Solution

  • I think you should split up the two concepts:

    • One class responsible for the timing (e.g. with start()/stopped()/elapsedMillis() methods)
    • One class responsible for producing interesting stats based on data

    The two are really pretty separate things, IMO - and definitely easier to test separately than together. You may well be able to find third party libraries for each separate component - or perhaps use an existing StopWatch class, but write your own stats cruncher.