Performance and optimization
Overview
Multik performance depends on three main factors:
Engine (KOTLIN vs NATIVE/OpenBLAS)
Data layout (contiguous arrays vs views)
Allocation patterns (temporary arrays and conversions)
This section highlights practical guidelines for common performance pitfalls.
Choose the right engine
DEFAULT picks the best available engine automatically.
KOTLIN is portable and works everywhere.
NATIVE uses OpenBLAS for faster linear algebra on supported platforms.
See Engines of Multik for selection details.
Prefer contiguous data
Multik uses a contiguous primitive array under the hood. Views (created by slicing or indexing) may be non-contiguous and can be slower to iterate.
Use deepCopy() to materialize a contiguous array when needed:
flatten() also returns a contiguous copy.
Minimize allocations
Each element-wise operation (+, -, *, /) creates a new array. If you are applying multiple operations, consider in-place updates when possible:
This avoids intermediate arrays but mutates the original data.
Be mindful of views
Indexing and slicing return views that share data with the base array. Views are cheap to create, but repeated operations over many small views can be costly. When performance matters, prefer working with a single contiguous array and use deepCopy() to detach from views if needed.
Reduce conversion overhead
Conversions allocate new memory:
toList,toArray,toIntArray, and similar helperscopy,deepCopy,flatten
Avoid repeated conversions inside tight loops. Instead, keep computations in ndarray form until the final output step.
Numeric type choices
Using smaller types (Float vs Double, Int vs Long) reduces memory traffic. Choose the smallest dtype that preserves the precision you need.