Skip to content

Performance of OuterProduct for SparseVectors #569

Open
@sebhofer

Description

@sebhofer

OuterProduct seems to be rather slow for large SparseVectors. Consider, for example,

let bar = SparseVector.init 5000 (fun i -> if i%100=0 then float i else 0.)
let mat1 = bar.OuterProduct(bar)
// Real: 00:00:00.809, CPU: 00:00:00.812, GC gen0: 0, gen1: 0, gen2: 0
let mat2 = (bar.ToColumnMatrix())*(bar.ToRowMatrix())
// Real: 00:00:00.000, CPU: 00:00:00.000, GC gen0: 0, gen1: 0, gen2: 0
mat1 = mat2

Things get worse when the vector gets denser:

let foo = SparseVector.init 200 (fun i -> float i)
let mat1 = foo.OuterProduct(foo)
//  Real: 00:00:00.410, CPU: 00:00:00.390, GC gen0: 0, gen1: 0, gen2: 0
let mat2 = (foo.ToColumnMatrix())*(foo.ToRowMatrix())
//  Real: 00:00:00.001, CPU: 00:00:00.000, GC gen0: 0, gen1: 0, gen2: 0
mat1 = mat2

Using matrix multiplication seems like a straightforward workaround. Am I missing something?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions