Java is one of the most widely used programming languages \u200b\u200bor at least the most famous. One weakness is the speed of implementation (which has been improved) and RAM memory consumption,
order to perform the famous "benchmarks" that can measure the execution time , this can be done through a class that simulates a stopwatch.
public class Stopwatch {private long startTime = -1;
stopTime private long = -1;
private boolean running = false;
public Stopwatch start(){
startTime = System.currentTimeMillis();
running = true;
return this;
}
public Stopwatch stop(){
stopTime = System.currentTimeMillis();
running = false;
return this;
}
public Stopwatch reset(){
startTime = -1;
stopTime = -1;
running = false;
return this;
}
public long getElapsedTime(){
if(startTime == -1)
return 0;
else if(running)
return System.currentTimeMillis() - startTime; Else return
stopTime - startTime;
}}
To use this class we just have to instantiate and call the necessary methods.
Example:
public static void main (String [] args) {
Stopwatch timer = new Stopwatch (). Start ();
long total = 0;
for (int i = 0; i Integer.MAX_VALUE; i + +) {
total + = i;}
System.out.println (timer.getElapsedTime ());}
0 comments:
Post a Comment