Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
    
I suspect that BinaryWriter, directly writing the stream or looking at BinaryFormatter might help. It seems unlikely that a "native python" operation on IronPython will be faster than anything .NET has to offer. What will happen to the data on the other end? –  Simon Opelt Feb 17 at 12:52
    
I will have a look into that. I would like to read the data from file in my C++ code. –  c_k Feb 17 at 13:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.