Skip to content

Instantly share code, notes, and snippets.

@jvillste
Created February 10, 2023 07:29
Show Gist options
  • Save jvillste/ad9f23e347dc29df3c0ccd86f0e89f57 to your computer and use it in GitHub Desktop.
Save jvillste/ad9f23e347dc29df3c0ccd86f0e89f57 to your computer and use it in GitHub Desktop.
benchmark of dynamic variable access in clojure
(ns dynamic-varialbe-access-benchmark
(:require [criterium.core :as criterium]))
(def ^:dynamic dynamic-x 1)
(def nondynamic-x 1)
(defn add-dynamic []
(+ 1 dynamic-x))
(defn add-nondynamic []
(+ 1 nondynamic-x))
(comment
(criterium/with-progress-reporting (criterium/quick-bench (add-dynamic)))
;; Warming up for JIT optimisations 5000000000 ...
;; compilation occurred before 3832 iterations
;; compilation occurred before 11494 iterations
;; compilation occurred before 26818 iterations
;; compilation occurred before 30649 iterations
;; compilation occurred before 471214 iterations
;; compilation occurred before 819835 iterations
;; compilation occurred before 873469 iterations
;; compilation occurred before 202583281 iterations
;; compilation occurred before 243119092 iterations
;; compilation occurred before 243122923 iterations
;; Estimating execution count ...
;; Sampling ...
;; Final GC...
;; Checking GC...
;; Finding outliers ...
;; Bootstrapping ...
;; Checking outlier significance
;; Evaluation count : 56853378 in 6 samples of 9475563 calls.
;; Execution time mean : 9.046112 ns
;; Execution time std-deviation : 0.029375 ns
;; Execution time lower quantile : 9.028166 ns ( 2.5%)
;; Execution time upper quantile : 9.095470 ns (97.5%)
;; Overhead used : 1.514405 ns
;; Found 1 outliers in 6 samples (16.6667 %)
;; low-severe 1 (16.6667 %)
;; Variance from outliers : 13.8889 % Variance is moderately inflated by outliers
(criterium/with-progress-reporting (criterium/quick-bench (add-nondynamic)))
;; Warming up for JIT optimisations 5000000000 ...
;; compilation occurred before 5457 iterations
;; compilation occurred before 71921 iterations
;; Estimating execution count ...
;; Sampling ...
;; Final GC...
;; Checking GC...
;; Finding outliers ...
;; Bootstrapping ...
;; Checking outlier significance
;; Evaluation count : 58881858 in 6 samples of 9813643 calls.
;; Execution time mean : 8.672314 ns
;; Execution time std-deviation : 0.015605 ns
;; Execution time lower quantile : 8.654678 ns ( 2.5%)
;; Execution time upper quantile : 8.696120 ns (97.5%)
;; Overhead used : 1.514405 ns
;; Found 1 outliers in 6 samples (16.6667 %)
;; low-severe 1 (16.6667 %)
;; Variance from outliers : 13.8889 % Variance is moderately inflated by outliers
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment