|
| 1 | + |
| 2 | +(ns benchmark |
| 3 | + (:require [criterium.core :as c]) |
| 4 | + (:import [java.util.concurrent Executors ]) |
| 5 | + ) |
| 6 | + |
| 7 | +(def thread-pool (Executors/newFixedThreadPool 10)) |
| 8 | + |
| 9 | +(defn math-direct [] |
| 10 | + (+ 1 1)) |
| 11 | + |
| 12 | +(defn math-future [] |
| 13 | + (deref |
| 14 | + (future (+ 1 1)))) |
| 15 | + |
| 16 | +(defn math-thread [] |
| 17 | + (let [result (atom nil)] |
| 18 | + (doto (Thread. (fn [] (reset! result (+ 1 1)))) |
| 19 | + (.start) |
| 20 | + (.join)) |
| 21 | + @result)) |
| 22 | + |
| 23 | +(defn math-threadpool [] |
| 24 | + (let [result (atom nil)] |
| 25 | + (.get (.submit thread-pool (fn [] (reset! result (+ 1 1))) )) |
| 26 | + @result)) |
| 27 | + |
| 28 | +(defn math-threadpool-no-atom [] |
| 29 | + (.get (.submit thread-pool (fn [] (+ 1 1)) ))) |
| 30 | + |
| 31 | + |
| 32 | +(c/bench (math-direct) ) |
| 33 | +; (out) Evaluation count : 6215391600 in 60 samples of 103589860 calls. |
| 34 | +; (out) Execution time mean : 2,015262 ns |
| 35 | +; (out) Execution time std-deviation : 0,497743 ns |
| 36 | +; (out) Execution time lower quantile : 1,442374 ns ( 2,5%) |
| 37 | +; (out) Execution time upper quantile : 3,392990 ns (97,5%) |
| 38 | +; (out) Overhead used : 7,915626 ns |
| 39 | +; (out) |
| 40 | +; (out) Found 5 outliers in 60 samples (8,3333 %) |
| 41 | +; (out) low-severe 3 (5,0000 %) |
| 42 | +; (out) low-mild 2 (3,3333 %) |
| 43 | +; (out) Variance from outliers : 94,6147 % Variance is severely inflated by outliers |
| 44 | + |
| 45 | +(c/bench (math-future) ) |
| 46 | +; (out) Evaluation count : 3735420 in 60 samples of 62257 calls. |
| 47 | +; (out) Execution time mean : 16,635809 µs |
| 48 | +; (out) Execution time std-deviation : 1,104338 µs |
| 49 | +; (out) Execution time lower quantile : 15,397518 µs ( 2,5%) |
| 50 | +; (out) Execution time upper quantile : 19,751883 µs (97,5%) |
| 51 | +; (out) Overhead used : 7,915626 ns |
| 52 | +; (out) |
| 53 | +; (out) Found 6 outliers in 60 samples (10,0000 %) |
| 54 | +; (out) low-severe 3 (5,0000 %) |
| 55 | +; (out) low-mild 3 (5,0000 %) |
| 56 | +; (out) Variance from outliers : 50,0892 % Variance is severely inflated by outliers |
| 57 | + |
| 58 | +(c/bench (math-thread)) |
| 59 | + |
| 60 | +; (out) Evaluation count : 774420 in 60 samples of 12907 calls. |
| 61 | +; (out) Execution time mean : 82,513236 µs |
| 62 | +; (out) Execution time std-deviation : 5,706987 µs |
| 63 | +; (out) Execution time lower quantile : 75,772237 µs ( 2,5%) |
| 64 | +; (out) Execution time upper quantile : 91,971212 µs (97,5%) |
| 65 | +; (out) Overhead used : 7,915626 ns |
| 66 | +; (out) |
| 67 | +; (out) Found 1 outliers in 60 samples (1,6667 %) |
| 68 | +; (out) low-severe 1 (1,6667 %) |
| 69 | +; (out) Variance from outliers : 51,7849 % Variance is severely inflated by outliers |
| 70 | + |
| 71 | +(c/bench (math-threadpool)) |
| 72 | +; (out) Evaluation count : 3815100 in 60 samples of 63585 calls. |
| 73 | +; (out) Execution time mean : 16,910124 µs |
| 74 | +; (out) Execution time std-deviation : 2,443261 µs |
| 75 | +; (out) Execution time lower quantile : 14,670118 µs ( 2,5%) |
| 76 | +; (out) Execution time upper quantile : 23,743868 µs (97,5%) |
| 77 | +; (out) Overhead used : 7,915626 ns |
| 78 | +; (out) |
| 79 | +; (out) Found 3 outliers in 60 samples (5,0000 %) |
| 80 | +; (out) low-severe 2 (3,3333 %) |
| 81 | +; (out) low-mild 1 (1,6667 %) |
| 82 | +; (out) Variance from outliers : 82,4670 % Variance is severely inflated by outliers |
| 83 | + |
| 84 | + |
| 85 | +(c/bench (math-threadpool-no-atom)) |
| 86 | + |
| 87 | +; (out) Evaluation count : 3794940 in 60 samples of 63249 calls. |
| 88 | +; (out) Execution time mean : 16,182655 µs |
| 89 | +; (out) Execution time std-deviation : 1,215451 µs |
| 90 | +; (out) Execution time lower quantile : 14,729393 µs ( 2,5%) |
| 91 | +; (out) Execution time upper quantile : 18,549902 µs (97,5%) |
| 92 | +; (out) Overhead used : 7,915626 ns |
| 93 | +; (out) |
| 94 | +; (out) Found 3 outliers in 60 samples (5,0000 %) |
| 95 | +; (out) low-severe 2 (3,3333 %) |
| 96 | +; (out) low-mild 1 (1,6667 %) |
| 97 | +; (out) Variance from outliers : 56,7625 % Variance is severely inflated by outliers |
0 commit comments