でlong型の配列としてのByteArrayを解釈することがmemoryview.cast()
介しバイトまたはint型またはlong型の配列として、基礎となるメモリを解釈することが可能である:のPython 3でのPython 2.7
[] b=bytearray(2*8)
[] b
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
[] m=memoryview(b).cast('L') #reinterpret as an array of unsigned longs
[] m[1]=2**64-1
[] b
bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff')
それは我々ができ、見ることができるようにbytearray
b
のように、memoryview
m
の助けを借りて、符号なしlong(私のマシンでは8バイト長)の配列と同じようにアクセスしてください。
しかし、Python 2.7では、memoryview
lacksの方法cast
です。
私の質問:bytearray
をPython 2.7のlong配列として再解析する可能性はありますか?
さらに多くのメモリをコピー/割り当てせずに行うことが重要です。私のマシン上でT[i]
経由long値を読み取るために必要な
時間(Pythonの3.4):
python list: 40ns (fastest but needs 24 bytes per element)
python array: 120ns (has to create python int-object)
memoryview of bytearray 120ns (the same as array.array)
Jean-François's solution: 6630ns (ca. 50 times slower)
Ross's solution: 120ns
申し訳ありませんが、私は私の質問に十分な明示的ではなかった、全体のポイントは、追加メモリなしでそれを行うことです。 – ead
問題はありませんが、私は 'pack 'でしばらく遊んでいました。私は後でカスタムソリューションを考え出すことができると思う。 –
が追加されました。スムーズではありませんが、Pythonで動作します2 –