Function in array of functions not defined


Sorry, I dont know if this question was answered already, but I wasn’t capable to find it.
I want to iterate over data which I want to apply to different functions, collected in an Array. In this case, I want to apply the macro benchmark to it and call a function with the specific value and the specific function to create an entry in a dict.

valuesForRun = [10000,100000,1000000,10000000,100000000]
    functions::Array{Function} = [simple_loop_sum, threads_sum, sharedarray_parallel_sum, sharedarray_mapreduce, pmap_sum_nb, pmap_sum_b]
    for value in valuesForRun
        for i=1:length(functions)
            fun::Function = functions[i]
            run = @benchmark fun(value);

I can iterate over data and functionArray and all println’s print the correct values, but if I try to call fun with a value, i get UndefVarError. Perhaps, there is easier way like map, also.


Try interpolating the function, too. Your code modified as an MWE:

using BenchmarkTools
valuesForRun = 10.^(5:8)
dummy1(n) = 1+n
dummy2(n) = sum(sqrt.(1:n))
functions = [dummy1, dummy2]
for value in valuesForRun
    for f in functions
        run = @benchmark $f($value);

Also, abstract type declarations are probably not buying you anything in terms of performance, so they can be omitted unless you have another reason to use them.


thank you for your advice,
can you please explain, why splicing inside the macro is necessary? I thought the function was unquoted and therefore dont need to be explicitly evaluated. Am I wrong?


See the BenchmarkTools.jl manual on interpolation, especially this:

Normally, you can’t use locally scoped variables in @benchmark or @benchmarkable, since all benchmarks are defined at the top-level scope by design. However, you can work around this by interpolating local variables into the benchmark expression:

and note that f is a local variable in your code.


Thank your for your help


You are probably looking for this.