Skip to content

Instantly share code, notes, and snippets.

@digash
Created October 17, 2009 21:49
Show Gist options
  • Save digash/212467 to your computer and use it in GitHub Desktop.
Save digash/212467 to your computer and use it in GitHub Desktop.
(ns l-cache)
(set! clojure.core/*warn-on-reflection* true)
(defmacro with-time
"Evaluates expr and returns a vector of expression value and the
time it took to evaluate it in milliseconds."
[expr]
`(let [start# (System/nanoTime)
ret# ~expr
t# (/ (double (- (System/nanoTime) start#)) 1e6)]
[ret# t#]))
(defn power-bytes [power]
(* (Math/pow 2 power) 1e3))
(defn fill-array! [array val]
(dotimes [i (count array)]
(aset array i val)))
(defn get-array [array]
(dotimes [i (count array)]
(aget array i)))
(defn copy-array [array]
(let [half-array-length (/ (count array) 2)]
(System/arraycopy array 0 array half-array-length half-array-length)))
;(copy-array (make-array Byte/TYPE 1e3))
;(byte (rand 256))
(def results
(for [p (range 9 15)]
(let [b (* (Math/pow 2 p) 1e3)
a (make-array Byte/TYPE b)
[_ t] (with-time (dotimes [_ 10] (copy-array a)))]
[b (/ t 10)])))
(println results)
;(reduce (fn [[b0 t0] [b1 t1]] [(/ b1 b0)]))
;;;; Copyright (c) 2009 Dimitry Gashinsky. No rights reserved.
;;;;
;;;; Author: Dimitry Gashinsky (dimitry :at gashinsky :dot com)
;;;; Id: c2f37c50-0c78-4973-9617-fbf91aeab8a6
(ns l-cache
(:use [incanter core charts stats]
[clojure.contrib pprint]))
(set! clojure.core/*warn-on-reflection* true)
(defmacro with-time
"Evaluates expr and returns a vector of expression value and the
time it took to evaluate it in milliseconds."
[expr]
`(let [start# (System/nanoTime)
ret# ~expr
t# (/ (double (- (System/nanoTime) start#)) 1e6)]
[ret# t#]))
(defn mirror-part-array [array length]
"Copy the first half of the array into the second."
(let [half-length (/ length 2)]
(System/arraycopy array 0 array half-length half-length)
half-length))
(def *lab-array* (make-array Byte/TYPE (Math/pow 2 26)))
(def results
(doall
(for [p (range 20 26) v (range -1e6 1e6 1e3)]
(let [b (+ (Math/pow 2 p) v)
[l t] (with-time (mirror-part-array *lab-array* b))]
[b (/ l t)]))))
;; (pprint results)
(def speed (map second results))
;; (view (xy-plot speed (variance speed)))
;; (reduce (fn [[b0 t0] [b1 t1]] [(/ b1 b0)]))
(set! clojure.core/*warn-on-reflection* false)
(def plot (xy-plot (map first results) (map second results)
:title "L1 and L2 Data Cache"
:x-label "bytes"
:y-label "bytes/milliseconds"))
(view plot)
(save plot
"/tmp/l-cache.png"
:width 1024
:height 768)
(view "file:///tmp/l-cache.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment