Friday, 4 May 2012

What table-like type has the fastest element lookup?

If there are just 2- or 3-elements then structs. For slightly more, use a reference type with explicit components. If the number of elements is more than about 30 then use a single array and do i + n*jyourself to index by row and column. Avoid .NET's 2D arrays because they are several times slower than necessary. Really avoid F#'s Matrix type for element-wise operations because it has comparatively huge overheads like dynamic dispatch (an order of magnitude slower). Arrays of arrays are good but indexing yourself permits more JIT optimizations of the indexing.

No comments: