Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
package org.immutant.core;
import org.immutant.runtime.ClojureRuntime;
import org.junit.Test;
import clojure.lang.RT;
import clojure.lang.Var;
public class ClojureRuntimeBenchmark {
int calls = 1000000;
@Test
public void benchmark() {
//warmup
direct( false );
runtime( false );
System.gc();
direct( true );
System.gc();
runtime( true );
}
void direct( boolean print ) {
Var v = RT.var("clojure.core", "str");
long t1 = System.nanoTime();
for( int i = 0; i < this.calls; i++ ) {
v.invoke( "a", "silly", "benchmark" );
}
if ( print ) {
printResults( t1, System.nanoTime(), "RT.var.invoke" );
}
}
void runtime( boolean print ) {
ClojureRuntime rt = ClojureRuntime.newRuntime( ClojureRuntime.class.getClassLoader(), "foo" );
long t1 = System.nanoTime();
for( int i = 0; i < this.calls; i++ ) {
rt.invoke( "clojure.core/str", "a", "silly", "benchmark" );
}
if ( print ) {
printResults( t1, System.nanoTime(), "ClojureRuntime.invoke" );
}
}
void printResults(long t1, long t2, String label) {
System.out.println(">>>> " + label + ": Execution time for " + this.calls + " calls: " +
((t2 - t1) * 1e-6) + "ms (" +
((t2 - t1) / calls) + "ns/call)" );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment