![](/img/trans.png)
[英]Fastest way to apply arithmetic operations to System.Array in IronPython
[英]How to efficiently write System.Array to a binary file in IronPython?
我必須將System.Array
(Single類型的1e09項)寫入二進制文件。 我當然可以使用.GetValue()
數組並將每個Single打包為4字節結構,但這非常慢。
在這種情況下可以使用標准的Python文件I / O嗎? 我嘗試了somefile_write(some_systemarray)
,但這會導致錯誤消息。
我主要對一維數組感興趣,但是非常適合nD數組的答案。
編輯閱讀第一條評論后,我嘗試了以下代碼:
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()
不幸的是,這需要花錢。 45秒 有BinaryWriter.Write(Byte[])
可用。 但是,只有在可以快速將System.Array
轉換為Byte[]
,這才會加快處理速度。
您是否嘗試過創建Byte數組而不是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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.