Create Stopwatch class with start, stop and deference methods -
It is use for find out the execution time of any process explicitly
public class StopWatch
{
long start = System.currentTimeMillis();
long stop = 0;
String title = null;
public StopWatch( String title )
{
this.title = title;
}
public long start()
{
start = System.currentTimeMillis();
stop = 0;
return start;
}
public long stop()
{
stop = System.currentTimeMillis();
return stop;
}
public long diff()
{
if ( stop == 0 )
{
return ( System.currentTimeMillis() - start );
}
return ( stop - start );
}
public static void main( String[] args )
{
StopWatch stopWatch = new StopWatch( "My Stop watch" );
System.out.println( "Start : " + stopWatch.start() );
System.out.println( "stop : " + stopWatch.stop() );
System.out.println( "difference : " + stopWatch.diff() );
System.out.println( stopWatch.toString() );
}
public String toString()
{
StringBuffer buffer = new StringBuffer( title );
buffer.append( " : Time Taken = " ).append( diff() ).append( " ms." );
return buffer.toString();
}
}
Output-
Start : 1590388113871
stop : 1590388113871
difference : 0
My Stop watch : Time Taken = 0 ms.
Comments
Post a Comment