I am optimizing a program I've been working on, and have hit a wall. The function julia-subrect maps over for-each-pixel a large number of times. I've optimized for-each-pixel to have a ~16x speedup. However, my optimized version of julia-subrect shows no evidence of this. Here are my benchmarks and relevant code:
; ======== Old `for-each-pixel` ======== ;(bench (julia/for-each-pixel (->Complex rc ic) max-itrs radius r-min x-step y-step [xt yt]))) ;Evaluation count : 3825300 in 60 samples of 63755 calls. ;Execution time mean : 16.018466 µs ; ======== New `for-each-pixel`. optimized 16x. ======== ;(bench (julia/for-each-pixel-opt [rc ic] [max-itrs radius r-min] [x-step y-step] [xt yt]))) ;Evaluation count : 59542860 in 60 samples of 992381 calls. ;Execution time mean : 1.038955 µs (defn julia-subrect [^Long start-x ^Long start-y ^Long end-x ^Long end-y ^Long total-width ^Long total-height ^Complex constant ^Long max-itrs] (let [grid (for [y (range start-y end-y)] (vec (for [x (range start-x end-x)] [x y]))) radius (calculate-r constant) r-min (- radius) r-max radius x-step (/ (Math/abs (- r-max r-min)) total-width) y-step (/ (Math/abs (- r-max r-min)) total-height) ; Uses old implementation of `for-each-pixel` calculate-pixel (partial for-each-pixel constant max-itrs radius r-min x-step y-step) for-each-row (fn [r] (map calculate-pixel r))] (map for-each-row grid))) ; ======== Old `julia-subrect` ======== ;(bench (doall (julia/julia-subrect start-x start-y end-x end-y total-width total-height c max-itrs)))) ;Evaluation count : 22080 in 60 samples of 368 calls. ;Execution time mean : 2.746852 ms (defn julia-subrect-opt [[^long start-x ^long start-y ^long end-x ^long end-y] [^double rc ^double ic] total-width total-height max-itrs ] (let [grid (for [y (range start-y end-y)] (vec (for [x (range start-x end-x)] [x y]))) radius (calculate-r-opt rc ic) r-min (- radius) r-max radius x-step (/ (Math/abs (- r-max r-min)) total-width) y-step (/ (Math/abs (- r-max r-min)) total-height) ;Uses new implementation of `for-each-pixel` calculate-pixel (fn [px] (for-each-pixel-opt [rc ic] [max-itrs radius r-min] [x-step y-step] px)) for-each-row (fn [r] (map calculate-pixel r))] (map for-each-row grid))) ; ======== New `julia-subrect`, but no speedup ======== ;(bench (doall (julia/julia-subrect-opt [start-x start-y end-x end-y] [rc ic] total-width total-height max-itrs)))) ;Evaluation count : 21720 in 60 samples of 362 calls. ;Execution time mean : 2.831553 ms Here is a gist containing source code for all the functions I've specified: https://gist.github.com/johnmarinelli/adc5533c19fb0b6d74cf4ef04ae55ee6
So, can anyone tell me why julia-subrect is showing no signs of speedup? Also, I'm still new to clojure so bear with me if the code is unidiomatic/ugly. Right now, I'm focusing on making the program run quicker.