Skip to content

100 times poorer performance indexing scalar record array (Trac #1386) #1984

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
thouis opened this issue Oct 19, 2012 · 6 comments · Fixed by #6208
Closed

100 times poorer performance indexing scalar record array (Trac #1386) #1984

thouis opened this issue Oct 19, 2012 · 6 comments · Fixed by #6208

Comments

@thouis
Copy link
Contributor

thouis commented Oct 19, 2012

Original ticket http://projects.scipy.org/numpy/ticket/1386 on 2010-01-31 by trac user oscar.bristol, assigned to unknown.

Assigning to a field in a scalar record array like this
B['a'] = A['a']
takes around 50 times as long as assigning like this:
B['a'][:] = A['a']
and 100 times as long as assigning to a size 1 record array.
I've attached a script (test.py) that compares scalars and size 1 arrays and the two different methods of indexing above. On my machine, the results are:

$ python test.py 100000
array, B['a'][:] = A['a'] 0.25 seconds
array, B['a'] = A['a'] 0.213 seconds
scalar, B['a'][:] = A['a'] 0.395 seconds
scalar, B['a'] = A['a'] 22.0 seconds

@thouis
Copy link
Contributor Author

thouis commented Oct 19, 2012

Attachment added by trac user oscar.bristol on 2010-01-31: test.py

@thouis
Copy link
Contributor Author

thouis commented Oct 19, 2012

Attachment added by trac user jpeel on 2011-01-12: 0001-BF-poor-performance-indexing-scalar-record-array.patch

@thouis
Copy link
Contributor Author

thouis commented Oct 19, 2012

trac user oscar.bristol wrote on 2010-01-31

(Second attempt)
Assigning to a field in a scalar record array like this:

B['a'][:] = A['a'][:]

takes around 50 times as long as assigning like this:

B['a'][:] = A['a']

and 100 times as long as assigning to a size 1 record array. I've attached a script (test.py) that compares scalars and size 1 arrays and the two different methods of indexing above. On my machine, the results are:

$ python test.py 100000 
array,  B['a'][:] = A['a'] 0.25 seconds
array,  B['a']    = A['a'] 0.213 seconds
scalar, B['a'][:] = A['a'] 0.395 seconds
scalar, B['a']    = A['a'] 22.0 seconds

@thouis
Copy link
Contributor Author

thouis commented Oct 19, 2012

trac user jpeel wrote on 2011-01-12

The problem was that the scalar void type's setfield method makes a copy of the input array even if it is already an array. I made a fast track for when the right hand side value is an array. The fast track uses the getfield method (which is used in the B['a'][:] scalar example above) to get a Numpy array for B['a']. I then just use PyArray_CopyObject to do the transfer. Now, this last method of assignment is right with the others in time (actually faster than scalar B['a'][:] because a slice is not done).

@thouis
Copy link
Contributor Author

thouis commented Oct 19, 2012

Milestone changed to NumPy 2.0 by trac user jpeel on 2011-01-12

mdboom pushed a commit to mdboom/numpy that referenced this issue Nov 15, 2012
@charris
Copy link
Member

charris commented Feb 20, 2014

Now it's only 10x as fast ;) @seberg @juliantaylor Either of you interested in taking a look at this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants