Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Initial commit; 2 scripts and README

  • Loading branch information...
commit caa4e23b28978b5ec7ef7d56f7c646cf44bb6182 0 parents
Stuart Sierra authored

Showing 3 changed files with 98 additions and 0 deletions. Show diff stats Hide diff stats

  1. +19 0 README.txt
  2. +10 0 run.sh
  3. +69 0 time-trials.clj
19 README.txt
... ... @@ -0,0 +1,19 @@
  1 +clojure-time-trials
  2 +
  3 +Clojure Time Trials
  4 +
  5 +by Stuart Sierra, http://stuartsierra.com/
  6 +
  7 +This repository contains tests to evaluate the effectiveness of
  8 +different optimization techniques on different versions of Clojure.
  9 +
  10 +HOW TO USE
  11 +
  12 +Clone this repository, then copy the JAR files for different versions
  13 +of Clojure into this directory. For example, I use
  14 +"clojure-1.0.0.jar", "clojure-1.1.0.jar", and
  15 +"clojure-1.2.0-master-SNAPSHOT.jar".
  16 +
  17 +The shell script "run.sh" will execute the same script
  18 +(time-trials.clj) once for each Clojure JAR in the directory, printing
  19 +the comparisons to standard output.
10 run.sh
... ... @@ -0,0 +1,10 @@
  1 +#!/bin/sh
  2 +
  3 +java -server -version
  4 +
  5 +for file in *.jar
  6 +do
  7 + java -server -cp $file clojure.main time-trials.clj
  8 +done
  9 +
  10 +
69 time-trials.clj
... ... @@ -0,0 +1,69 @@
  1 +(defmacro nanotime [reps body]
  2 + `(let [start# (System/nanoTime)]
  3 + (dotimes [i# ~reps] ~body)
  4 + (/ (- (System/nanoTime) start#) 1000000.0)))
  5 +
  6 +(defmacro time-trials [sets reps body]
  7 + `(for [i# (range ~sets)] (nanotime ~reps ~body)))
  8 +
  9 +(defn clojure-version-string []
  10 + (str (:major *clojure-version*) \.
  11 + (:minor *clojure-version*) \.
  12 + (:incremental *clojure-version*)
  13 + (when-let [q (:qualifier *clojure-version*)]
  14 + (when-not (= q "") (str \- q)))))
  15 +
  16 +(defmacro compare-times
  17 + "Measures time for n repetitions of each body, averaged over k runs
  18 + (omits first 2 runs from average). Prints description, followed by
  19 + averages from body1 and body2."
  20 + [description k n body1 body2]
  21 + `(let [times1# (drop 2 (time-trials ~k ~n ~body1))
  22 + times2# (drop 2 (time-trials ~k ~n ~body2))]
  23 + (println (format "%30s %9.2f %9.2f" ~description
  24 + (/ (apply + times1#) ~(- k 2))
  25 + (/ (apply + times2#) ~(- k 2))))))
  26 +
  27 +(println (format "%-30s %9s %9s" (str "# Clojure " (clojure-version-string))
  28 + "BEFORE" "AFTER"))
  29 +
  30 +(let [s (identity "Hello, World!")]
  31 + (compare-times "Eliminate Reflection" 5 10000
  32 + (.length s)
  33 + (.length #^String s)))
  34 +
  35 +(compare-times "Use primitives in loops"
  36 + 5 100000
  37 + (loop [sum 0, x 1]
  38 + (if (= x 100)
  39 + sum
  40 + (recur (+ sum x) (inc x))))
  41 + (loop [sum (int 0), x (int 1)]
  42 + (if (= x 100)
  43 + sum
  44 + (recur (unchecked-add sum x) (unchecked-inc x)))))
  45 +
  46 +
  47 +(compare-times "Replace = with =="
  48 + 5 1000000
  49 + (= 2 4) (== 2 4))
  50 +
  51 +(compare-times "Use Binary Arithmetic"
  52 + 5 1000000
  53 + (+ 2 4 6 8) (+ 2 (+ 4 (+ 6 8))))
  54 +
  55 +(let [v [1 2 3]]
  56 + (compare-times "Avoid Destructuring"
  57 + 5 1000000
  58 + (let [[x y z] v] (+ x y z))
  59 + (let [x (nth v 0)
  60 + y (nth v 1)
  61 + z (nth v 2)] (+ x y z))))
  62 +
  63 +(def *value* 42)
  64 +
  65 +(let [value *value*]
  66 + (compare-times "Avoid Var lookups"
  67 + 5 1000000
  68 + (* *value* *value*)
  69 + (* value value)))

0 comments on commit caa4e23

Please sign in to comment.
Something went wrong with that request. Please try again.