[Numpy-discussion] My identity
Keith Goodman
kwgoodman at gmail.com
Mon Jul 20 12:45:03 EDT 2009
On Mon, Jul 20, 2009 at 9:32 AM, Keith Goodman<kwgoodman at gmail.com> wrote:
> On Mon, Jul 20, 2009 at 9:03 AM, Charles R
> Harris<charlesr.harris at gmail.com> wrote:
>>
>>
>> On Mon, Jul 20, 2009 at 9:39 AM, Keith Goodman <kwgoodman at gmail.com> wrote:
>>>
>>> Using a trick that Robert Kern recently posted to the list makes the
>>> identity function much faster.
>>>
>>> Current version:
>>>
>>> def identity(n, dtype=None):
>>> a = array([1]+n*[0],dtype=dtype)
>>> b = empty((n,n),dtype=dtype)
>>> b.flat = a
>>> return b
>>>
>>> Proposed version:
>>>
>>> def myidentity(n, dtype=None):
>>> a = zeros((n,n), dtype=dtype)
>>> a.flat[::n+1] = 1
>>> return a
>>>
>>> >> timeit identity(1)
>>> 100000 loops, best of 3: 14.9 µs per loop
>>> >> timeit identity(10)
>>> 10000 loops, best of 3: 20 µs per loop
>>> >> timeit identity(100)
>>> 1000 loops, best of 3: 696 µs per loop
>>> >> timeit identity(1000)
>>> 10 loops, best of 3: 73.6 ms per loop
>>>
>>> >> timeit myidentity(1)
>>> 100000 loops, best of 3: 6.57 µs per loop
>>> >> timeit myidentity(10)
>>> 100000 loops, best of 3: 7.08 µs per loop
>>> >> timeit myidentity(100)
>>> 100000 loops, best of 3: 16.4 µs per loop
>>> >> timeit myidentity(1000)
>>> 100 loops, best of 3: 5.92 ms per loop
>>>
>>> It would also speed up the functions that use identity (for example
>>> np.linalg.inv). And it would make identity faster than eye.
>>>
>>> Are there any corner cases that the new version doesn't handle? Any
>>> other issues?
>>
>> I believe r7130 made the change.
>
> I don't see it here (r7153):
>
> http://svn.scipy.org/svn/numpy/trunk/numpy/core/numeric.py
>
> Am I looking in the wrong place?
Oh, sorry, I misunderstood. Yes, a similar change was made to eye but
not to identity.
More information about the NumPy-Discussion
mailing list