Modified permuteExistingNumbers() to pre-allocate sufficiently large slice for all...
authorAaron Taylor <ataylor@subgeniuskitty.com>
Sat, 15 May 2021 22:15:08 +0000 (15:15 -0700)
committerAaron Taylor <ataylor@subgeniuskitty.com>
Sat, 15 May 2021 22:15:08 +0000 (15:15 -0700)
commit8be725f2ca1fc13ac01f8f3a553e6a8803d08d91
tree57150141d43e6b99b33d886f514e3673572e33ed
parentc3b4805541f68c1dccc4e7007fdb7c828bb86f8f
Modified permuteExistingNumbers() to pre-allocate sufficiently large slice for all results.

Based on profiling, significant time was spent in growSlice() and memMove() as
a result of the per-number slice append(). Consolidating the allocations resulted
in significant speed and memory usage improvements.

Benchmarking command:
    time ./ch1-breeding-numbers -generations 13 -silent
    go tool pprof ch1-breeding-numbers 13gen-memprofile-gen12.mprof
        top25

Before:
    71.978u 3.726s 0:31.79 238.0%   0+0k 40+75io 353pf+0w
    Showing nodes accounting for 3.70GB, 100% of 3.70GB total

After:
    38.303u 2.233s 0:23.41 173.1%   0+0k 40+62io 354pf+0w
    Showing nodes accounting for 2.29GB, 100% of 2.29GB total
chapter-1-experiments/ch1-breeding-numbers.go