NumPy arrays with SQLite

后端 未结 4 615
孤城傲影
孤城傲影 2021-02-04 19:39

The most common SQLite interface I\'ve seen in Python is sqlite3, but is there anything that works well with NumPy arrays or recarrays? By that I mean one that reco

4条回答
  •  梦如初夏
    2021-02-04 19:50

    Doug's suggestion with redis is quite good, but I think his code is a bit complicated and, as a result, rather slow. For my purposes, I had to serialize+write and then grab+deserialize a square matrix of about a million floats in less than a tenth of a second, so I did this:

    For writing:

    snapshot = np.random.randn(1024,1024)
    serialized = snapshot.tobytes()
    rs.set('snapshot_key', serialized)
    

    Then for reads:

    s = rs.get('snapshot_key')
    deserialized = np.frombuffer(s).astype(np.float32)
    rank = np.sqrt(deserialized.size).astype(int)
    snap = deserialized(rank, rank)
    

    You can do some basic performance testing with ipython using %time, but neither the tobytes or frombuffer take more than a few milliseconds.

提交回复
热议问题