Fastest way to apply arithmetic operations to System.Array in IronPython
Asked Answered
W

1

8

I would like to add (arithmetics) two large System.Arrays element-wise in IronPython and store the result in the first array like this:

    for i in range(0:ArrA.Count) :
      arrA.SetValue(i, arrA.GetValue(i) + arrB.GetValue(i));

However, this seems very slow. Having a C background I would like to use pointers or iterators. However, I do not know how I should apply the IronPython idiom in a fast way. I cannot use Python lists, as my objects are strictly from type System.Array. The type is 3d float.

What is the fastests / a fast way to perform to compute this computation?

Edit:

  • The number of elements is appr. 256^3.
  • 3d float means that the array can be accessed like this: array.GetValue(indexX, indexY, indexZ). I am not sure how the respective memory is organized in IronPython's System.Array.
  • Background: I wrote an interface to an IronPython API, which gives access to data in a simulation software tool. I retrieve 3d scalar data and accumulate it to a temporal array in my IronPython script. The accumulation is performed 10,000 times and should be fast, so that the simulation does not take ages.
Willful answered 28/1, 2015 at 14:23 Comment(2)
Could you provide more context? 3d float? A struct? What is the actual operation? 3 adds? How many elements is large? How often does the operation occur?Evildoer
Extra context provided.Willful
Q
0

Is it possible to use the numpy library developed for IronPython?

https://pytools.codeplex.com/wikipage?title=NumPy%20and%20SciPy%20for%20.Net

It appears to be supported, and as far as I know is as close you can get in python to C style pointer functionality with arrays and such.

Create an array:

x = np.array([[1, 2, 3], [4, 5, 6]], np.int32)

Multiply all elements by 3.0:

x *= 3.0
Quincy answered 23/2, 2015 at 21:6 Comment(4)
Unfortunately, as I am interfacing an API I cannot go away from System.Array. Converting to numpy first would not make it faster, really?Willful
Can you try the generic access methods IE for i in range(0:ArrA.Count) : arrA[i] = arrA[i] + arrB[i]? Also compare with full on converting to numpy, then back to the array. Who knows? :)Quincy
The generic access methods do not work on 3D arrays. As far as I know, for converting to numpy I have to do the same (time expensive loop) as shown in my question.Willful
try x = np.array(ArrA) to initialize the array to numpy. Also I feel that the array you are using does not separate into tuples, so you have to use 3 gets.Quincy

© 2022 - 2024 — McMap. All rights reserved.