Optimizing Disk I/O in NumPy with LZ4 C-Extensions
Описание и применение
Статья описывает библиотеку numpy-cache, которая использует C-расширения и алгоритм LZ4 для быстрого кэширования массивов NumPy. Это решение устраняет компромисс между скоростью записи и размером файла, предлагая производительность, близкую к нестиснутому сохранению, но с лучшей компрессией, чем стандартный zlib.
Задача
Developers face a trade-off between disk I/O speed and storage space when handling large NumPy arrays. Standard np.save() is fast but uncompressed, while np.savez_compressed() is slow due to single-threaded DEFLATE, and HDF5/Zarr introduce heavy dependencies.
Результат
The implementation provides a lightweight solution that combines the speed of direct memory access with efficient LZ4 compression. It reduces write latency significantly compared to standard compressed formats while maintaining smaller file sizes than uncompressed formats.
Рабочий процесс
- Install the numpy-cache library.
- Import the
saveandloadfunctions fromnumpy_cache. - Call
save(array, filename)to compress and write the array using LZ4. - Optionally adjust the
accelerationparameter (1-16) to trade compression ratio for speed. - Call
load(filename)to decompress and restore the array with original dtype and shape.
Доказательства
numpy_cache: ~100 ms for 100 MB array
20 × faster than np.savez_compressed()
- Дата записи: 2026-09-09T01:00:17.659Z