Created
May 17, 2012 20:45
-
-
Save tobias/2721494 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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