I have to write a System.Array
(1e09 items from type Single) to a binary file. I could of course loop over the Array with .GetValue()
and pack each Single in a 4-Byte struct, but this is very slow.
Is it possible to use the standard Python file i/o in this case? I have tried somefile_write(some_systemarray)
, but this results in an error message.
I am mainly interested in 1D arrays, however an answer which works for nD arrays would be highly appreciated.
Edit After reading the first comment, I have tried the following code:
from System import *
from System.IO import *
arr = Array.CreateInstance(Single, 1e8)
b = BinaryWriter(File.Open('test.bin', FileMode.Create))
for i in arr :
b.Write(i)
b.Close()
Unfortunately, this takes appr. 45 sec. There is BinaryWriter.Write(Byte[])
available. However, this will only speed up things when it is possible to convert System.Array
to Byte[]
quickly.
Have you tried creating an array of Byte instead of an array of Single?
from System import *
from System.IO import *
arr = Array.CreateInstance(Byte, 1e8)
b = BinaryWriter(File.Open('test.bin', FileMode.Create))
b.Write(arr)
b.Close()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.