Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use of global variables to avoid recomputing solutions #120

Open
natema opened this issue Oct 13, 2022 · 2 comments
Open

Use of global variables to avoid recomputing solutions #120

natema opened this issue Oct 13, 2022 · 2 comments
Labels
improvement Improve existing code

Comments

@natema
Copy link
Member

natema commented Oct 13, 2022

We currently make use of global variables to avoid recomputing some solutions: see e.g. here:

function crowdingsolution()
    isdefined(@__MODULE__, :_solution_crowding) && return _solution_crowding
    global _solution_crowding = solve(crowding(), (1900, 2300))
    return _solution_crowding
end

After discussing the problem in the Discourse, we might consider using a closure as a more elegant approach:

let isfirstcall = true, output = 0
   global function crowdingsolution()             
       isfirstcall || return output
       isfirstcall = false
       return output = solve(crowding(), (1900, 2300))
   end
end
@natema natema added the improvement Improve existing code label Oct 13, 2022
@natema
Copy link
Member Author

natema commented Oct 13, 2022

I did a test out of curiosity and it seems that the closure is faster:

julia> function f_global()
           isdefined(@__MODULE__, :_output) && return _output
           global _output = 42
           return _output
       end
f_global (generic function with 1 method)

julia> f_global()
42

julia> @benchmark f_global()
BenchmarkTools.Trial: 10000 samples with 999 evaluations.
 Range (min  max):  12.185 ns  54.210 ns  ┊ GC (min  max): 0.00%  0.00%
 Time  (median):     13.612 ns              ┊ GC (median):    0.00%
 Time  (mean ± σ):   13.818 ns ±  1.758 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

         ▃▂▇▅█▇▄▄▁                                             
  ▂▂▂▄▃▆▇██████████▇▅▅▃▃▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▁▂▂▂▂▂▂▂▁▂▂▂▂ ▃
  12.2 ns         Histogram: frequency by time        19.4 ns <

 Memory estimate: 0 bytes, allocs estimate: 0.

julia> let isfirstcall = true, output = 0
          global function f_closure()             
              isfirstcall || return output
              isfirstcall = false
              return output = 42
          end
       end
f_closure (generic function with 1 method)

julia> f_closure()
42

julia> @benchmark f_closure()
BenchmarkTools.Trial: 10000 samples with 1000 evaluations.
 Range (min  max):  1.622 ns  3.031 ns  ┊ GC (min  max): 0.00%  0.00%
 Time  (median):     1.663 ns             ┊ GC (median):    0.00%
 Time  (mean ± σ):   1.661 ns ± 0.060 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

                                       ▂▄▄▄▃█                
  ▁▁▁▁▂▃▄▄▄▅▆▅▄▃▄▂▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁▂▃▃▇▇████████▅▄▃▂▂▂▂▂▂▂▂▂▂ ▃
  1.62 ns        Histogram: frequency by time       1.68 ns <

 Memory estimate: 0 bytes, allocs estimate: 0.

@paulobruno
Copy link
Collaborator

Wow, it was a big difference!
It shouldn't be hard to change the current code, since we don't have so many solutions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
improvement Improve existing code
Projects
None yet
Development

No branches or pull requests

2 participants