[Numpy-discussion] python numpy code many times slower than c++

Neal Becker ndbecker2@gmail....
Wed Jan 21 15:34:35 CST 2009

Neal Becker wrote:

> Ravi wrote:
>> On Wednesday 21 January 2009 13:55:49 Neal Becker wrote:
>>> I'm only interested in simple strided 1-d vectors.  In that case, I
>>> think your code already works.  If you have c++ code using the iterator
>>> interface, the iterators dereference will  use (*array )[index].  This
>>> will use operator[], which will call PyArray_GETPTR.  So I think this
>>> will obey strides.
>> You are right. I had forgotten that I had simple strided vectors working.
>>> Unfortunately, it will also be slow.  I suggest something like the
>>> enclosed.   I have done some simple tests, and it seems to work.
>> I wonder why PyArray_GETPTR1 is slow. Is it because of the implied
>> integer multiplication? Unfortunately, your approach means that iterators
>> can become invalid if the underlying array is resized to a larger size.
>> Hmmh, perhaps we could make this configurable at compile-time ...

Iterators almost always become invalid under those sorts of changes, so I 
don't think that's a surprise.

GETPTR1 has to do:
There's several memory references there, and I don't think the compiler can 
assume that this value doesn't change from one access to another - so it 
can't be cached.

That said, I have tried a few benchmarks.  Surprisingly, I'm not seeing any 
difference in a few quick tests.

I do have one cosmetic patch for you.  This will shutup gcc giving the 
longest warning message ever about an unused variable:
--- numpy.new.orig/numpyregister.hpp	2009-01-21 15:59:00.000000000 -0500
+++ numpy.new/numpyregister.hpp	2009-01-21 14:11:00.000000000 -0500
@@ -257,7 +257,8 @@
     storage_t *the_storage = reinterpret_cast<storage_t*>( data );
     void *memory_chunk = the_storage->storage.bytes;
     array_storage_t dd( obj );
-    array_t *v = new ( memory_chunk ) array_t( dd.size(), dd );
+    //array_t *v = new ( memory_chunk ) array_t( dd.size(), dd );
+    new ( memory_chunk ) array_t( dd.size(), dd );
     data->convertible = memory_chunk;

More information about the Numpy-discussion mailing list