core: added bench module

This commit is contained in:
hellerve 2017-10-25 13:07:34 +02:00
parent a8d354d45e
commit a8d0eef5ba
4 changed files with 44 additions and 0 deletions

View File

@ -164,6 +164,7 @@ preludeModules carpDir = map (\s -> carpDir ++ "/core/" ++ s ++ ".carp") [ "Macr
, "Geometry"
, "Test"
, "Statistics"
, "Bench"
]
main :: IO ()

35
core/Bench.carp Normal file
View File

@ -0,0 +1,35 @@
(defmodule Bench
(system-include "bench.h")
(register get-time-elapsed (Fn [] Double))
(defn get-unit [n]
(cond
(> n 1000.0) (String.append (Double.str n) "µs")
(> n 1000000.0) (String.append (Double.str (/ n 1000.0)) "ms")
(> n 1000000000.0) (String.append (Double.str (/ n 1000000.0)) "s")
(String.append (Double.str (/ n 1000000000.0)) "s")))
(defn print [title n]
(let [unit (get-unit n)]
(do
(IO.println title)
(IO.println &unit))))
(defmacro bench [n form]
(list 'let ['before (Bench.get-time-elapsed)
'times []]
(list 'do
(list 'for ['i 0 n]
(list 'let ['before-once (Bench.get-time-elapsed)]
(list 'do
form
(list 'set! &times (Array.push-back (Array.copy &times) (Double.- (get-time-elapsed) before-once))))))
(list 'let ['total (Double.- (Bench.get-time-elapsed) before)
'per (list 'Double./ 'total (list 'Double.from-int n))]
(do
(Bench.print "Total time elapsed: " total)
(Bench.print "Time elapsed per run (average): " per)
(Bench.print "Best case: " (Statistics.min &times))
(Bench.print "Worst case: " (Statistics.max &times))
(Bench.print "Standard deviation: " (Statistics.stdev &times)))))))

View File

@ -17,5 +17,6 @@
(register inc (λ [Long] Long))
(register dec (λ [Long] Long))
(register copy (λ [&Long] Long)) ;; TODO: Should not be needed when refs to value types are auto-converted to non-refs.
(register from-int (λ [Int] Long))
)

7
core/bench.h Normal file
View File

@ -0,0 +1,7 @@
#include <sys/time.h>
double get_MINUS_time_MINUS_elapsed() {
struct timeval tv;
gettimeofday(&tv, NULL);
return 1000000 * tv.tv_sec + tv.tv_usec;;
}