Skip to content

Instantly share code, notes, and snippets.

@tobias
Created May 17, 2012 20:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobias/2721494 to your computer and use it in GitHub Desktop.
Save tobias/2721494 to your computer and use it in GitHub Desktop.
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