When testing runtime, I use System.nanotime() in the following way:
startTime = System.nanotime();
// some statements
System.out.println("Runtime: " + (System.nanoTime() - startTime));
Is there a way to reuse this test model for other blocks of code in my programs? In other words, can I create this as a method and pass other methods to it during testing?
Yes, you can, but it’s even more complicated than your approach:
In Java 7, this will get a bit simpler (I left out the ‘Measure’ implementation):
By the way, it’s better to use
System.nanoTime()thanSystem.currentTimeMillis(), not only because the former is more accurate, but also because the later will hickup around summertime change (or other system time changes).