bench: added first bits of benchmarking

This commit is contained in:
hellerve 2018-01-25 17:26:20 +01:00
parent 026251a241
commit 9a3b0e5f59
4 changed files with 123 additions and 3 deletions

100
bench/arithmetic.carp Normal file
View File

@ -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)))

19
bench/array_access.carp Normal file
View File

@ -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)))

View File

@ -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]

View File

@ -194,3 +194,6 @@
(defmacro println* [:rest forms]
(list 'IO.println (list 'ref (build-str* forms))))
(defmacro ignore [form]
(let [_ form] ()))