From 9a3b0e5f59f12fe50eed36646d9c51c58b1e480c Mon Sep 17 00:00:00 2001 From: hellerve Date: Thu, 25 Jan 2018 17:26:20 +0100 Subject: [PATCH] bench: added first bits of benchmarking --- bench/arithmetic.carp | 100 ++++++++++++++++++++++++++++++++++++++++ bench/array_access.carp | 19 ++++++++ core/Bench.carp | 4 +- core/Macros.carp | 3 ++ 4 files changed, 123 insertions(+), 3 deletions(-) create mode 100644 bench/arithmetic.carp create mode 100644 bench/array_access.carp diff --git a/bench/arithmetic.carp b/bench/arithmetic.carp new file mode 100644 index 00000000..955e18a3 --- /dev/null +++ b/bench/arithmetic.carp @@ -0,0 +1,100 @@ +(load "Bench.carp") +(use Bench) + +(defn add-int [] (+ 100 100)) +(defn sub-int [] (- 100 100)) +(defn mul-int [] (* 100 100)) +(defn div-int [] (/ 100 100)) +(defn mod-int [] (mod 100 100)) +(defn random-int [] (Int.random)) + +(defn add-long [] (+ 100l 100l)) +(defn sub-long [] (- 100l 100l)) +(defn mul-long [] (* 100l 100l)) +(defn div-long [] (/ 100l 100l)) +(defn mod-long [] (mod 100l 100l)) +(defn random-long [] (Long.random)) + +(defn add-double [] (+ 100.0 100.0)) +(defn sub-double [] (- 100.0 100.0)) +(defn mul-double [] (* 100.0 100.0)) +(defn div-double [] (/ 100.0 100.0)) +(defn mod-double [] (mod 100.0 100.0)) +(defn random-double [] (Double.random)) + +(defn add-float [] (+ 100.0f 100.0f)) +(defn sub-float [] (- 100.0f 100.0f)) +(defn mul-float [] (* 100.0f 100.0f)) +(defn div-float [] (/ 100.0f 100.0f)) +(defn mod-float [] (mod 100.0f 100.0f)) +(defn random-float [] (Float.random)) + +(defn int-tests [] + (do + (IO.println "Adding ints:") + (bench add-int) + (IO.println "\nSubtracting ints:") + (bench sub-int) + (IO.println "\nMultiplying ints:") + (bench mul-int) + (IO.println "\nDividing ints:") + (bench div-int) + (IO.println "\nModulo of ints:") + (bench mod-int) + (IO.println "\nGenerating a random int:") + (bench random-int))) + +(defn long-tests [] + (do + (IO.println "Adding longs:") + (bench add-long) + (IO.println "\nSubtracting longs:") + (bench sub-long) + (IO.println "\nMultiplying longs:") + (bench mul-long) + (IO.println "\nDividing longs:") + (bench div-long) + (IO.println "\nModulo of longs:") + (bench mod-long) + (IO.println "\nGenerating a random long:") + (bench random-long))) + +(defn double-tests [] + (do + (IO.println "Adding doubles:") + (bench add-double) + (IO.println "\nSubtracting doubles:") + (bench sub-double) + (IO.println "\nMultiplying doubles:") + (bench mul-double) + (IO.println "\nDividing doubles:") + (bench div-double) + (IO.println "\nModulo of doubles:") + (bench mod-double) + (IO.println "\nGenerating a random double:") + (bench random-double))) + +(defn float-tests [] + (do + (IO.println "Adding floats:") + (bench add-float) + (IO.println "\nSubtracting floats:") + (bench sub-float) + (IO.println "\nMultiplying floats:") + (bench mul-float) + (IO.println "\nDividing floats:") + (bench div-float) + (IO.println "\nModulo of floats:") + (bench mod-float) + (IO.println "\nGenerating a random float:") + (bench random-float))) + +(defn main [] + (do + (int-tests) + (IO.println "") + (long-tests) + (IO.println "") + (double-tests) + (IO.println "") + (float-tests))) diff --git a/bench/array_access.carp b/bench/array_access.carp new file mode 100644 index 00000000..5814c976 --- /dev/null +++ b/bench/array_access.carp @@ -0,0 +1,19 @@ +(load "Bench.carp") +(use Bench) + +(def int-arr [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20]) +(def arr-arr [[1 2 3 4 5] [1 2 3 4 5] [6 7 8 9 10] [6 7 8 9 10]]) +(def str-arr [@"str1" @"str2" @"str3" @"str4" @"str5"]) + +(defn int-access [] (ignore (Array.nth &int-arr 3))) +(defn arr-access [] (ignore (Array.nth &arr-arr 3))) +(defn str-access [] (ignore (Array.nth &str-arr 3))) + +(defn main [] + (do + (IO.println "Array of Ints access times:") + (bench int-access) + (IO.println "\nArray of Arrays access times:") + (bench arr-access) + (IO.println "\nArray of Strings access times:") + (bench str-access))) diff --git a/core/Bench.carp b/core/Bench.carp index a5e3b111..5e3e22d5 100644 --- a/core/Bench.carp +++ b/core/Bench.carp @@ -27,9 +27,7 @@ (defn ns-iter-inner [f n] (let [start (get-time-elapsed)] (do - (for [i 0 n] - (let [x (f)] ; this little trick guarantees that f can be any snippet and return whatever - ())) + (for [i 0 n] (ignore f)) (Double.- (get-time-elapsed) start)))) (defn print-bench-results [res total] diff --git a/core/Macros.carp b/core/Macros.carp index 8094e033..9116cba2 100644 --- a/core/Macros.carp +++ b/core/Macros.carp @@ -194,3 +194,6 @@ (defmacro println* [:rest forms] (list 'IO.println (list 'ref (build-str* forms)))) + +(defmacro ignore [form] + (let [_ form] ()))