JuliaMath / HCubature.jl

pure-Julia multidimensional h-adaptive integration
Other
153 stars 25 forks source link

Very minor speed-up in countevals #2

Closed giordano closed 7 years ago

giordano commented 7 years ago

countevals in genz-malik.jl is very cheap and is called just once for each call to hcubature, so improving it isn't definitely a priority. However, its result can be cached by making countevals a generated function, for example in this way:

get_dimensions(::Type{GenzMalik{n,T}}) where {n,T} = n
@generated function countevals(g::GenzMalik{n}) where {n}
    m = get_dimensions(g)
    1 + 4m + 2*m*(m-1) + (1<<m)
end

I didn't open a PR because I'm not sure this is really worth.

stevengj commented 7 years ago

Actually, I think LLVM already does this for you. Since n is a compile-time constant, its constant-folding optimizations eliminate all of the arithmetic:

julia> g = HCubature.GenzMalik(Val{3});

julia> @code_llvm HCubature.countevals(g)

define i64 @julia_countevals_60874(i8** dereferenceable(80)) #0 !dbg !5 {
top:
  ret i64 33
}
giordano commented 7 years ago

Good! Actually, I did see an improvement when benchmarking the two implementations (well, with my patch runtime of countevals went from ~1 ns to ~0.01 ns), so I thought that there was no optimization in place in current implementation. My bad for not having checked llvm code.