AtomicFrontierCode / keyboards

Simulated annealing code for video
MIT License
274 stars 32 forks source link

Performance fix #9

Closed YingboMa closed 7 months ago

YingboMa commented 7 months ago

This PR speeds up the script by about 70x.

Before doing the performance optimization, I changed the script to use StableRNGs.jl so that the result will be stable across runs to make the performance comparison fair. To avoid print excessively, I added the verbose option. Also, I added save_current_best to avoid generating too many intermediate PNGs. Finally, I compared both bestGenomes.txt, and the optimized output matches with the unoptimized output. Thus, the optimization is valid.

Before:

julia> begin
           isfile("bestGenomes.txt") && rm("bestGenomes.txt")
           seed = 123456
           Random.seed!(rng, seed)
           @time runSA(;
              save_current_best = false,
              verbose = false,
           )
       end;
160.339287 seconds (1.92 G allocations: 66.786 GiB, 1.05% gc time, 0.02% compilation time)

Now:

julia> begin
           isfile("bestGenomes.txt") && rm("bestGenomes.txt")
           seed = 123456
           Random.seed!(rng, seed)
           @time runSA(;
              save_current_best = false,
              verbose = false,
           )
       end;
  2.221573 seconds (1.08 M allocations: 83.932 MiB, 0.28% gc time)