There are several parts that have an effect here:
1) The time to define the function (create the function object),
2) The time to look up the function object by name,
3) The time to actually call the function.
Your global function example is faster with 1) (no need to redefine a
in each call to b1
). However, it is slower with in 2) because global variable lookup is slower than local lookup.
Why can't we have both then?
I've extended your benchmark with a solution that uses the global function, but avoids the global lookup using a local variable. It seems to be the fastest of the three on my machine:
5 6 7
b1: 0.04147 0.44421 4.46508
b2: 0.03399 0.43321 4.41121
b3: 0.03258 0.41821 4.25542
b1: 0.03240 0.42998 4.39774
b2: 0.03320 0.43465 4.42229
b3: 0.03155 0.42109 4.23669
b1: 0.03273 0.43321 4.37266
b2: 0.03326 0.43551 4.42208
b3: 0.03137 0.42356 4.25341
b1: 0.03253 0.43104 4.40466
b2: 0.03401 0.43719 4.42996
b3: 0.03155 0.41681 4.24132
b1: 0.03244 0.42965 4.37192
b2: 0.03310 0.43629 4.42727
b3: 0.03117 0.41701 4.23932