giordano / Cuba.jl

Library for multidimensional numerical integration with four independent algorithms: Vegas, Suave, Divonne, and Cuhre.
https://giordano.github.io/Cuba.jl/stable
MIT License
75 stars 9 forks source link

make getindex shorter and faster #9

Closed afniedermayer closed 7 years ago

afniedermayer commented 7 years ago

getfield(x, n) has the same effect as getfield(x, fieldname(Integral, n)), but is faster:

julia> using BenchmarkTools                                                                                                                                                                         

julia> immutable Foo                                                                                                                                                                                
         x::Int64                                                                                                                                                                                   
         y::Float64                                                                                                                                                                                 
       end                                                                                                                                                                                          

julia> foo=Foo(2,3)                                                                                                                                                                                 
Foo(2,3.0)                                                                                                                                                                                          

julia> @benchmark getfield(foo, 1)                                                                                                                                                                  
BenchmarkTools.Trial:                                                                                                                                                                               
  memory estimate:  0 bytes                                                                                                                                                                         
  allocs estimate:  0                                                                                                                                                                               
  --------------                                                                                                                                                                                    
  minimum time:     16.891 ns (0.00% GC)                                                                                                                                                            
  median time:      16.904 ns (0.00% GC)                                                                                                                                                            
  mean time:        17.243 ns (0.00% GC)                                                                                                                                                            
  maximum time:     43.376 ns (0.00% GC)                                                                                                                                                            
  --------------                                                                                                                                                                                    
  samples:          10000                                                                                                                                                                           
  evals/sample:     998                                                                                                                                                                             

julia> @benchmark getfield(foo, fieldname(Foo, 1))                                                                                                                                                  
BenchmarkTools.Trial:                                                                                                                                                                               
  memory estimate:  0 bytes                                                                                                                                                                         
  allocs estimate:  0                                                                                                                                                                               
  --------------                                                                                                                                                                                    
  minimum time:     23.665 ns (0.00% GC)                                                                                                                                                            
  median time:      23.709 ns (0.00% GC)                                                                                                                                                            
  mean time:        24.441 ns (0.00% GC)                                                                                                                                                            
  maximum time:     128.074 ns (0.00% GC)                                                                                                                                                           
  --------------                                                                                                                                                                                    
  samples:          10000                                                                                                                                                                           
  evals/sample:     996                                                                                                                                                                             
giordano commented 7 years ago

Good catch! Thank you so much.