Last active
December 13, 2018 11:45
-
-
Save jgpc42/dda3d793487e92ef462c5549d8510fb6 to your computer and use it in GitHub Desktop.
JMH Clojure overhead test
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
;; src/demo/core.clj | |
(ns demo.core) | |
(defn add-boxed [a b] | |
(+ a b)) | |
(defn add-prim ^long [^long a ^long b] | |
(unchecked-add a b)) | |
(defn random-long [] | |
(rand Long/MAX_VALUE)) |
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
{:benchmarks [{:fn demo.core/add-boxed, :args [:long, :long]} | |
{:fn demo.core/add-prim, :args [:long, :long]}] | |
:states {:long {:fn demo.core/random-long, :scope :thread}}} |
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
// src/demo/Native.java | |
package demo; | |
import java.util.concurrent.ThreadLocalRandom; | |
import org.openjdk.jmh.annotations.*; | |
public class Native { | |
@Benchmark | |
public Object addBoxed (LongState a, LongState b) { | |
return a.boxedValue + b.boxedValue; | |
} | |
@Benchmark | |
public long addPrim (LongState a, LongState b) { | |
return a.primValue + b.primValue; | |
} | |
@State(Scope.Thread) | |
public static class LongState { | |
public final Long boxedValue; | |
public final long primValue; | |
public LongState () { | |
boxedValue = primValue = ThreadLocalRandom.current().nextLong(Long.MAX_VALUE); | |
} | |
} | |
} |
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
(defproject demo "0.1.0-SNAPSHOT" | |
:dependencies [[org.clojure/clojure "1.8.0"] | |
[org.openjdk.jmh/jmh-core "1.19"]] | |
:plugins [[lein-jmh "0.1.0"]] | |
:java-source-paths ["src"] | |
:profiles {:jmh {:jvm-opts []}}) |
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
({:fn demo.core/add-boxed, :score [1.34282604683602E8 "ops/s"]} | |
{:fn demo.core/add-prim, :score [3.9702571820788E8 "ops/s"]} | |
{:method demo.Native/addBoxed, :score [1.83245610798165E8 "ops/s"]} | |
{:method demo.Native/addPrim, :score [3.97514308593916E8 "ops/s"]}) | |
({:fn demo.core/add-boxed, :score [1.34596275373956E8 "ops/s"]} | |
{:fn demo.core/add-prim, :score [3.5972258512132E8 "ops/s"]} | |
{:method demo.Native/addBoxed, :score [1.82422272720768E8 "ops/s"]} | |
{:method demo.Native/addPrim, :score [3.95996384333695E8 "ops/s"]}) | |
({:fn demo.core/add-boxed, :score [1.29409395391849E8 "ops/s"]} | |
{:fn demo.core/add-prim, :score [3.97471110595301E8 "ops/s"]} | |
{:method demo.Native/addBoxed, :score [1.8285168470296E8 "ops/s"]} | |
{:method demo.Native/addPrim, :score [3.97095081749897E8 "ops/s"]}) | |
({:fn demo.core/add-boxed, :score [1.34428090482388E8 "ops/s"]} | |
{:fn demo.core/add-prim, :score [3.97099480376152E8 "ops/s"]} | |
{:method demo.Native/addBoxed, :score [1.82323578600328E8 "ops/s"]} | |
{:method demo.Native/addPrim, :score [3.97690206053138E8 "ops/s"]}) | |
({:fn demo.core/add-boxed, :score [1.76108890554332E8 "ops/s"]} | |
{:fn demo.core/add-prim, :score [3.96770743953259E8 "ops/s"]} | |
{:method demo.Native/addBoxed, :score [1.83169450586705E8 "ops/s"]} | |
{:method demo.Native/addPrim, :score [3.97466361792193E8 "ops/s"]}) | |
({:fn demo.core/add-boxed, :score [1.34467456536801E8 "ops/s"]} | |
{:fn demo.core/add-prim, :score [3.97534031694686E8 "ops/s"]} | |
{:method demo.Native/addBoxed, :score [1.80771182000043E8 "ops/s"]} | |
{:method demo.Native/addPrim, :score [3.9769516345109E8 "ops/s"]}) | |
({:fn demo.core/add-boxed, :score [1.29645473772757E8 "ops/s"]} | |
{:fn demo.core/add-prim, :score [3.97536607875924E8 "ops/s"]} | |
{:method demo.Native/addBoxed, :score [1.81241046318114E8 "ops/s"]} | |
{:method demo.Native/addPrim, :score [3.97498382883899E8 "ops/s"]}) | |
({:fn demo.core/add-boxed, :score [1.34385278489153E8 "ops/s"]} | |
{:fn demo.core/add-prim, :score [3.97365406664843E8 "ops/s"]} | |
{:method demo.Native/addBoxed, :score [1.81348681649366E8 "ops/s"]} | |
{:method demo.Native/addPrim, :score [3.97288017212726E8 "ops/s"]}) | |
({:fn demo.core/add-boxed, :score [1.71061922736567E8 "ops/s"]} | |
{:fn demo.core/add-prim, :score [3.97824269609597E8 "ops/s"]} | |
{:method demo.Native/addBoxed, :score [1.81396414751836E8 "ops/s"]} | |
{:method demo.Native/addPrim, :score [3.97514954222955E8 "ops/s"]}) | |
({:fn demo.core/add-boxed, :score [1.34480335495819E8 "ops/s"]} | |
{:fn demo.core/add-prim, :score [3.9750725124648E8 "ops/s"]} | |
{:method demo.Native/addBoxed, :score [1.81803128506296E8 "ops/s"]} | |
{:method demo.Native/addPrim, :score [3.97395605686628E8 "ops/s"]}) |
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
lein javac | |
iterations=${1:-10} | |
opts='{ | |
:externs [demo.Native] | |
:only [:fn :method :score] | |
:pprint true | |
:type :quick | |
}' | |
for _ in $(seq "$iterations"); do | |
lein jmh "$opts" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment