Skip to content

Commit aec1fa2

Browse files
author
Alireza Alavi
committed
perf(d2p1): further optimization
1 parent b79dbe1 commit aec1fa2

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed
763 KB
Loading

src/aoc/2025/day2/main.clj

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,41 @@
216216
; ```
217217
; Boy, Oh boy! We got ~87.3% increase in performance after our optimizations!\
218218
; These are still cookie-cutter optimizations, tough. I think we can get much further
219-
; with more in depth opts, but I don't know how to.
219+
; with more in depth opts.
220+
221+
; Our profiling after the optimizations shows that now our most resource consuming
222+
; part is the `mapv` function in `num->digits-opt`.
223+
(clerk/image "src/aoc/2025/day2/assets/profiling2.png")
224+
225+
; Let's see how we can improve it.
226+
;
227+
; Now that I look at it, the previous `num->digits-opt` looks absolutely stupid.\
228+
; There are two calls to `str` and there is no reason to use threading `->>` here.
229+
; Just makes it confusing.
230+
; - Convert `n` to string directly, feed it as `coll` to `mapv`.
231+
; - Use `int` substract the ASCII offset to get the number.
232+
233+
(defn num->digits-opt2
234+
[^long n]
235+
(mapv #(- (int %) 48) (Long/toString n)))
236+
237+
; ```clojure
238+
; (with-redefs-fn {#'repeated-twice? repeated-twice-opt?
239+
; #'num->digits num->digits-opt2}
240+
; #(c/quick-bench (sum-invalid-ids (parse-input "src/aoc/2025/day2/input.txt"))))
241+
; =>
242+
; (out) Evaluation count : 6 in 6 samples of 1 calls.
243+
; (out) Execution time mean : 927.130309 ms
244+
; (out) Execution time std-deviation : 30.063344 ms
245+
; (out) Execution time lower quantile : 898.622286 ms ( 2.5%)
246+
; (out) Execution time upper quantile : 972.890815 ms (97.5%)
247+
; (out) Overhead used : 6.691813 ns
248+
; (out)
249+
; (out) Found 1 outliers in 6 samples (16.6667 %)
250+
; (out) low-severe 1 (16.6667 %)
251+
; (out) Variance from outliers : 13.8889 % Variance is moderately inflated by outliers
252+
; ```
253+
; This make our total optimizations **~90% faster** than our initial solution.
220254

221255
; ## Running all tests
222256
(run-tests)

0 commit comments

Comments
 (0)