|
216 | 216 | ; ``` |
217 | 217 | ; Boy, Oh boy! We got ~87.3% increase in performance after our optimizations!\ |
218 | 218 | ; 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. |
220 | 254 |
|
221 | 255 | ; ## Running all tests |
222 | 256 | (run-tests) |
0 commit comments