Skip to content

Conversation

snoeyink
Copy link
Contributor

The SMatrix{4,4} det code (added in #260) performs 72 multiplications and 23 addition/subtractions. Rewriting as products of 2x2 minors reduces to 30 multiplications and 17 additions/subtractions. Benchmarking (below) shows performance improvement from 11ns to <5ns, commensurate with the decrease in operations.

I see that in pull request #597 @ryanelandt (with the approval of @andyferris) implicitly rewrote inv using 2x2 minors. Thus, my pull request just extends the performance improvement to det.


julia> using BenchmarkTools, Random, LinearAlgebra                                                        
                                                                                                          
julia> using StaticArrays                                                                                 
[ Info: Precompiling StaticArrays [90137ffa-7385-5640-81b9-e52037218182]                                  
                                                                                                          
julia> function bmold()                                                                                   
       N=4                                                                                                
       Random.seed!(1234)                                                                                 
       SA = @SMatrix rand(N,N)                                                                            
       A = Array(SA)                                                                                      
       @assert(det(SA)  det(A))                                                                          
       @benchmark StaticArrays._olddet($(Size(SA)), $(SA))                                                
       end                                                                                                
bmold (generic function with 1 method)                                                                    
                                                                                                          
julia> function bm()                                                                                      
       N=4                                                                                                
       Random.seed!(1234)                                                                                 
       SA = @SMatrix rand(N,N)                                                                            
       A = Array(SA)                                                                                      
       @assert(det(SA)  det(A))                                                                          
       @benchmark StaticArrays._det($(Size(SA)), $(SA))                                                   
       end                                                                                                
bm (generic function with 1 method)                                                                       

julia> bmold()
BenchmarkTools.Trial: 10000 samples with 999 evaluations.
Range (min … max): 10.975 ns … 47.887 ns ┊ GC (min … max): 0.00% … 0.00%
Time (median): 11.259 ns ┊ GC (median): 0.00%
Time (mean ± σ): 11.398 ns ± 1.592 ns ┊ GC (mean ± σ): 0.00% ± 0.00%
Memory estimate: 0 bytes, allocs estimate: 0.

julia> bm()
BenchmarkTools.Trial: 10000 samples with 1000 evaluations.
Range (min … max): 4.582 ns … 34.553 ns ┊ GC (min … max): 0.00% … 0.00%
Time (median): 4.707 ns ┊ GC (median): 0.00%
Time (mean ± σ): 4.770 ns ± 0.865 ns ┊ GC (mean ± σ): 0.00% ± 0.00%
Memory estimate: 0 bytes, allocs estimate: 0.

Copy link
Collaborator

@mateuszbaran mateuszbaran left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, looks good 👍 .

@mateuszbaran mateuszbaran merged commit 139df3f into JuliaArrays:master Jul 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants