Fastest way to store a numpy array in redis

后端 未结 5 521
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-05 16:08

I\'m using redis on an AI project.

The idea is to have multiple environment simulators running policies on a lot of cpu cores. The simulators write experience (a li

5条回答
  •  感情败类
    2020-12-05 17:01

    You could also consider using msgpack-numpy, which provides "encoding and decoding routines that enable the serialization and deserialization of numerical and array data types provided by numpy using the highly efficient msgpack format." -- see https://msgpack.org/.

    Quick proof-of-concept:

    import msgpack
    import msgpack_numpy as m
    import numpy as np
    m.patch()               # Important line to monkey-patch for numpy support!
    
    from redis import Redis
    
    r = Redis('127.0.0.1')
    
    # Create an array, then use msgpack to serialize it 
    d_orig = np.array([1,2,3,4])
    d_orig_packed = m.packb(d_orig)
    
    # Set the data in redis
    r.set('d', d_orig_packed)
    
    # Retrieve and unpack the data
    d_out = m.unpackb(r.get('d'))
    
    # Check they match
    assert np.alltrue(d_orig == d_out)
    assert d_orig.dtype == d_out.dtype
    

    On my machine, msgpack runs much quicker than using struct:

    In: %timeit struct.pack('4096L', *np.arange(0, 4096))
    1000 loops, best of 3: 443 µs per loop
    
    In: %timeit m.packb(np.arange(0, 4096))
    The slowest run took 7.74 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000 loops, best of 3: 32.6 µs per loop
    

提交回复
热议问题