Skip to content

Instantly share code, notes, and snippets.

@jlchan
Created May 22, 2024 14:56
Show Gist options
  • Save jlchan/9d2ad444a4de7f22b57ef7d4d51c3252 to your computer and use it in GitHub Desktop.
Save jlchan/9d2ad444a4de7f22b57ef7d4d51c3252 to your computer and use it in GitHub Desktop.
using LinearAlgebra
using StaticArrays
using BenchmarkTools
function time_SVector_3arg_mul(b, A, x)
mul!(b, A, x)
end
function time_SVector_5arg_mul(b, A, x, alpha, beta)
mul!(b, A, x, alpha, beta)
end
function time_Vector_3arg_mul(b1, b2, A, x1, x2)
mul!(b1, A, x1)
mul!(b2, A, x2)
end
function time_Vector_5arg_mul(b1, b2, A, x1, x2, alpha, beta)
mul!(b1, A, x1, alpha, beta)
mul!(b2, A, x2, alpha, beta)
end
for N in [10 15 20 35]
A = randn(N, N)
x = [SVector(1.0, 2.0) for _ in axes(A, 2)]
b = similar(x)
x1, x2 = [getindex.(x, i) for i in 1:2]
b1, b2 = similar.((x1, x2))
println("==== On N = $N ====")
println("SVector mul! timings are")
@btime time_SVector_3arg_mul($b, $A, $x)
@btime time_SVector_5arg_mul($b, $A, $x, $.1, $.5)
println("Vector mul! timings are")
@btime time_Vector_3arg_mul($b1, $b2, $A, $x1, $x2)
@btime time_Vector_5arg_mul($b1, $b2, $A, $x1, $x2, $.1, $.5)
println("")
end
# Results:
# ==== On N = 10 ====
# SVector mul! timings are
# 57.977 ns (0 allocations: 0 bytes)
# 73.356 ns (1 allocation: 32 bytes)
# Vector mul! timings are
# 140.434 ns (0 allocations: 0 bytes)
# 137.862 ns (0 allocations: 0 bytes)
# ==== On N = 15 ====
# SVector mul! timings are
# 123.608 ns (0 allocations: 0 bytes)
# 140.637 ns (1 allocation: 32 bytes)
# Vector mul! timings are
# 214.298 ns (0 allocations: 0 bytes)
# 212.368 ns (0 allocations: 0 bytes)
# ==== On N = 20 ====
# SVector mul! timings are
# 202.477 ns (0 allocations: 0 bytes)
# 219.212 ns (1 allocation: 32 bytes)
# Vector mul! timings are
# 215.278 ns (0 allocations: 0 bytes)
# 209.742 ns (0 allocations: 0 bytes)
# ==== On N = 35 ====
# SVector mul! timings are
# 578.066 ns (0 allocations: 0 bytes)
# 594.737 ns (1 allocation: 32 bytes)
# Vector mul! timings are
# 485.608 ns (0 allocations: 0 bytes)
# 496.778 ns (0 allocations: 0 bytes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment