Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial commit; 2 scripts and README

  • Loading branch information...
commit caa4e23b28978b5ec7ef7d56f7c646cf44bb6182 0 parents
Stuart Sierra authored
Showing with 98 additions and 0 deletions.
  1. +19 −0 README.txt
  2. +10 −0 run.sh
  3. +69 −0 time-trials.clj
19 README.txt
@@ -0,0 +1,19 @@
+clojure-time-trials
+
+Clojure Time Trials
+
+by Stuart Sierra, http://stuartsierra.com/
+
+This repository contains tests to evaluate the effectiveness of
+different optimization techniques on different versions of Clojure.
+
+HOW TO USE
+
+Clone this repository, then copy the JAR files for different versions
+of Clojure into this directory. For example, I use
+"clojure-1.0.0.jar", "clojure-1.1.0.jar", and
+"clojure-1.2.0-master-SNAPSHOT.jar".
+
+The shell script "run.sh" will execute the same script
+(time-trials.clj) once for each Clojure JAR in the directory, printing
+the comparisons to standard output.
10 run.sh
@@ -0,0 +1,10 @@
+#!/bin/sh
+
+java -server -version
+
+for file in *.jar
+do
+ java -server -cp $file clojure.main time-trials.clj
+done
+
+
69 time-trials.clj
@@ -0,0 +1,69 @@
+(defmacro nanotime [reps body]
+ `(let [start# (System/nanoTime)]
+ (dotimes [i# ~reps] ~body)
+ (/ (- (System/nanoTime) start#) 1000000.0)))
+
+(defmacro time-trials [sets reps body]
+ `(for [i# (range ~sets)] (nanotime ~reps ~body)))
+
+(defn clojure-version-string []
+ (str (:major *clojure-version*) \.
+ (:minor *clojure-version*) \.
+ (:incremental *clojure-version*)
+ (when-let [q (:qualifier *clojure-version*)]
+ (when-not (= q "") (str \- q)))))
+
+(defmacro compare-times
+ "Measures time for n repetitions of each body, averaged over k runs
+ (omits first 2 runs from average). Prints description, followed by
+ averages from body1 and body2."
+ [description k n body1 body2]
+ `(let [times1# (drop 2 (time-trials ~k ~n ~body1))
+ times2# (drop 2 (time-trials ~k ~n ~body2))]
+ (println (format "%30s %9.2f %9.2f" ~description
+ (/ (apply + times1#) ~(- k 2))
+ (/ (apply + times2#) ~(- k 2))))))
+
+(println (format "%-30s %9s %9s" (str "# Clojure " (clojure-version-string))
+ "BEFORE" "AFTER"))
+
+(let [s (identity "Hello, World!")]
+ (compare-times "Eliminate Reflection" 5 10000
+ (.length s)
+ (.length #^String s)))
+
+(compare-times "Use primitives in loops"
+ 5 100000
+ (loop [sum 0, x 1]
+ (if (= x 100)
+ sum
+ (recur (+ sum x) (inc x))))
+ (loop [sum (int 0), x (int 1)]
+ (if (= x 100)
+ sum
+ (recur (unchecked-add sum x) (unchecked-inc x)))))
+
+
+(compare-times "Replace = with =="
+ 5 1000000
+ (= 2 4) (== 2 4))
+
+(compare-times "Use Binary Arithmetic"
+ 5 1000000
+ (+ 2 4 6 8) (+ 2 (+ 4 (+ 6 8))))
+
+(let [v [1 2 3]]
+ (compare-times "Avoid Destructuring"
+ 5 1000000
+ (let [[x y z] v] (+ x y z))
+ (let [x (nth v 0)
+ y (nth v 1)
+ z (nth v 2)] (+ x y z))))
+
+(def *value* 42)
+
+(let [value *value*]
+ (compare-times "Avoid Var lookups"
+ 5 1000000
+ (* *value* *value*)
+ (* value value)))
Please sign in to comment.
Something went wrong with that request. Please try again.