[an error occurred while processing this directive]

This page describes methods to measure the runtime of methods in Java. [] (timethese time these in Java measure the runtime of a method run time performance currrentTimeMillis class source code benchmark benchmarking), report, page 722083
https://www.purl.org/stefan_ram/pub/timethese-in-java (canonical URI).
Stefan Ram

Measuring Run-Times with Java

TimeTheses

The following main method uses the static method "timethese" to find the runtime of several methods. The method "timethese" will time the runtime of all declared methods of the object that is the second argument, while the first argument is specifying how often each invocation is to be repeated.

TimeThese.java
public class TimeThese 
{ final static int size = 100;
public static void main( final java.lang.String[] _ ) 
{ timethese( 1000000, new java.lang.Object()  
{ int dummy = 0; final java.util.List a;
{ a = new java.util.ArrayList( size ); 
for( int i = 0; i < size; ++i ) 
a.add( new java.lang.Integer( i )); }
void calc() { for( int i = 0; i < a.size(); ++i ) 
dummy +=(( java.lang.Integer )a.get( i )).intValue(); }
void len() { for (int i = 0, len = a.size(); i < len; i++ ) 
dummy +=(( java.lang.Integer )a.get( i )).intValue(); }
void down() { for (int i = a.size() - 1; i >= 0; i-- ) 
dummy +=(( java.lang.Integer )a.get( i )).intValue(); }}); }
public static void timethese 
( final long count, final java.lang.Object o ) 
{ try{ java.lang.Class c = o.getClass(); 
java.lang.reflect.Method m[] = c.getDeclaredMethods(); 
for( int i = 0; i < m.length; ++i )  
{ java.lang.reflect.Method m0 = m[ i ]; 
java.lang.System.out.println( m0.getName() ); 
long a = java.lang.System.nanoTime(); 
for( long j = count; j-- > 0; )m0.invoke( o, null ); 
long b = java.lang.System.nanoTime(); 
java.lang.System.out.println( b - a ); }}  
catch( final java.lang.Throwable t ) 
{ java.lang.System.err.println( t ); }}}

System.out
len 
562045124 
calc 
390348480 
down 
300342781

TimeThese1

The following is a somewhat more elaborated version. I have not yet found the time to comment the details.

It sorts the results and formats the output more nicely.

And it has an outer loop around everything so that all methods are treated more equal with regard to HotSpot  optimization.

TimeThese1.java
class TimeThings 
{ final java.lang.String newLine;
public TimeThings( final java.lang.String newLine ) 
{ this.newLine = newLine; }
public java.lang.String timethese 
( final long count, final java.lang.Object o ) 
{ java.lang.String report = ""; 
final java.util.Map<java.lang.String,java.lang.Long> sum = 
new java.util.HashMap<java.lang.String,java.lang.Long>(); 
try{ final java.lang.Class c = o.getClass(); 
final java.lang.reflect.Method m[] = c.getDeclaredMethods(); 
final int mLength = m.length;  
for( int i = 0; i < mLength; ++i ) 
{ sum.put( m[ i ].getName(), 0L ); } 
for( int r = 0; r < 100; ++r ) 
{ for( int i = 0; i < mLength; ++i )  
{ final java.lang.reflect.Method m_ = m[ i ]; 
final java.lang.String mName = m_.getName(); 
final long a = System.nanoTime(); 
for( long j = count; j-- > 0; )m_.invoke( o, null ); 
final long dt = System.nanoTime() - a; 
sum.put( mName, sum.get( mName )+ dt ); }} 
final java.util.TreeMap<Long,java.lang.String> sorted = 
new java.util.TreeMap<Long,java.lang.String>(); 
int mxLen = -1; for( int i = 0; i < mLength; ++i ) 
{ sorted.put( new Long( sum.get( m[ i ].getName() ), i ), m[ i ].getName() );  
final int len = m[ i ].getName().length() + 
java.lang.String.format( "%d", sum.get( m[ i ].getName() )).length(); 
if( len > mxLen )mxLen = len; } 
for( Long l : sorted.keySet() ) 
{ final java.lang.String name = sorted.get( l ); 
final int len =( name + java.lang.String.format( "%d", l.longValue() ) ).length();  
report += name +  
java.lang.String.format("%" +( mxLen - len + 1 )+ "s", "" ) + 
java.lang.String.format( "%d", l.longValue() ) + newLine; }}  
catch( final Throwable t ){ System.err.println( t ); } return report; }}
public class TimeThese1 
{ public static void main( final java.lang.String[] _ ) 
{ TimeThings timeThings = new TimeThings( java.lang.String.format( "%n" )); 
java.lang.System.out.println 
( timeThings.timethese( 1000, new Object()  
{ final int size = 10000; 
java.util.List a; { a = new java.util.ArrayList( size ); 
for(int i = 0; i < size; ++i )a.add( new java.lang.Integer( i )); } 
void nop(){} 
int nop1(){ return (( java.lang.Integer )a.get( 0 )).intValue(); } 
int calc(){ int s = 0; for( int i = 0; i < a.size(); ++i ) 
s += (( java.lang.Integer )a.get( i )).intValue(); return s; } 
int len(){ int s = 0; final int aSize = a.size(); 
for( int i = 0 ; i < aSize; ++i ) 
s += (( java.lang.Integer )a.get( i )).intValue(); return s; } 
int down(){ int s = 0; for( int i = a.size(); --i >= 0; ) 
s += (( java.lang.Integer )a.get( i )).intValue(); return s; } 
int unroll()  
{ int sa = 0, sb = 0, sc = 0, sd = 0, se = 0, i = a.size() - 1; 
for( ;i >= 4; i -= 5 )  
{ sa +=(( java.lang.Integer )a.get( i )).intValue(); 
sb +=(( java.lang.Integer )a.get( i - 1 )).intValue(); 
sc +=(( java.lang.Integer )a.get( i - 2 )).intValue(); 
sd +=(( java.lang.Integer )a.get( i - 3 )).intValue(); 
se +=(( java.lang.Integer )a.get( i - 4 )).intValue(); } 
for( ; i >= 0; --i )sa +=(( java.lang.Integer )a.get( i )).intValue(); 
int s = sa + sb + sc + sd + se; return s; }})); }}
class Long implements java.lang.Comparable<Long> 
{ final java.lang.Long me; final java.lang.Integer bias;
public Long( final java.lang.Long me, final int bias ) 
{ this.me = me; this.bias = new java.lang.Integer( bias ); }
public int compareTo( final Long other ) 
{ final int product = this.me.compareTo( other.me );  
final int product1 = 
product == 0 ? -this.bias.compareTo( other.bias ) : product; 
return product1; }
public long longValue(){ return this.me.longValue(); }}

System.out
nop1    54363127 
nop 91908176 
down 239960440 
calc 264872022 
len 306160015 
unroll 398913832

About this page, Impressum  |   Form for messages to the publisher regarding this page  |   "ram@zedat.fu-berlin.de" (without the quotation marks) is the email-address of Stefan Ram.   |   Beginning at the start page often more information about the topics of this page can be found. (A link to the start page appears at the very top of this page.)  |   Copyright 2004 Stefan Ram, Berlin. All rights reserved. This page is a publication by Stefan Ram. slrprd, PbclevtugFgrsnaEnz