Let's load some packages

using OneHot, CairoMakie, Random, BenchmarkTools

Setup the size of the examples we'll look at

q = 20
N = 200
M = 400

Benchmark normal matrix multiply. This doesn't exploit OneHot structure of X.

@benchmark A * X setup=(A=randn(M,q); X=Array(OneHotArray(rand(1:q, N), q)))
BenchmarkTools.Trial: 2521 samples with 1 evaluation.
 Range (min … max):  1.872 ms …   4.198 ms  ┊ GC (min … max): 0.00% … 46.38%
 Time  (median):     1.907 ms               ┊ GC (median):    0.00%
 Time  (mean ± σ):   1.926 ms ± 150.337 μs  ┊ GC (mean ± σ):  0.49% ±  3.39%

     ▁▄▆█▅▄▁                                                   
  ▂▄▆███████▆▅▃▃▂▂▂▂▂▂▂▂▂▁▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▂▁▂▂▂▂▂▂▂▂ ▃
  1.87 ms         Histogram: frequency by time        2.18 ms <

 Memory estimate: 646.88 KiB, allocs estimate: 5.

This package implements efficient matrix multiply by a OneHotArray.

@benchmark A * X setup=(A=randn(M,q); X=OneHotArray(rand(1:q, N), q))
BenchmarkTools.Trial: 10000 samples with 1 evaluation.
 Range (min … max):   96.301 μs …   2.797 ms  ┊ GC (min … max): 0.00% … 95.53%
 Time  (median):     120.302 μs               ┊ GC (median):    0.00%
 Time  (mean ± σ):   132.324 μs ± 124.457 μs  ┊ GC (mean ± σ):  6.29% ±  6.55%

     ▃█▅                                                         
  ▂▃▇███▄▃▂▂▂▂▁▁▁▂▁▁▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▂▁▁▁▁▂▁▂▂▂▂▂▂ ▂
  96.3 μs          Histogram: frequency by time          417 μs <

 Memory estimate: 625.05 KiB, allocs estimate: 2.

That's about 20x faster! We can also check that these two codes are computing the same thing.

A = randn(M, q)
X = OneHotArray(rand(1:q, N), q)
A * X ≈ A * Array(X)
true

This page was generated using Literate.jl.