The answer is that numpy arrays exist because they can store homogeneous arrays of values efficiently directly in memory, and numpy functions can then manipulate these value efficiently -- for instance, a type check only needs to be done once for an entire array operation, rather than for each element.
Cython simply lets you access either regular python data structures or numpy arrays (Or other C data structures) and write C code to work with the data. but the real difference you're seeing in your examples, is the difference between raw python lists and functions and numpy.
BTW, you could code up a Cython sum() that worked with regular python lists, and I expect is would be about the same as python's built-in sum()
-CHB
--
---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Chris Barker schrieb am 11.04.2016 um 18:30:
> BTW, you could code up a Cython sum() that worked with regular python
> lists, and I expect is would be about the same as python's built-in sum()
... except that Python's sum() is really smart and well written.
It does
its computation in C space as long as it can (no pun intended) and only
gradually switches to less efficient implementations as the values grow,
with very little overhead.
https://hg.python.org/cpython/file/e3c9a47a83fb/Python/bltinmodule.c#l2179
So you'd already have to implement pretty much the same algorithm to even
come close, and then rely on Cython's fast for-loop implementation and
inlined integer unpacking to *maybe* make a tiny difference. Assuming you
can even use them, given how specialised the original implementation is.
Stefan
--
---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
In [1]: import numba as nb
In [2]: @nb.jit(nopython=True)
...: def sumr(i):
...: s=0
...: for x in range(i):
...: s+=x
...: return s
In [3]: r1=sum(range(int(1e6)))
In [4]: r2=sumr(int(1e6))
In [5]: import numpy as np
In [6]: r3=np.sum(np.arange(0,int(1e6)))
In [7]: r1==r2
Out[7]: True
In [8]: r3==r2
Out[8]: True
In [9]: %timeit r1=sum(range(int(1e6)))
10 loops, best of 3: 37.3 ms per loop
In [10]: %timeit r2=sumr(int(1e6))
The slowest run took 17.59 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 592 ns per loop
In [11]: %timeit np.sum(np.arange(0,int(1e6)))
The slowest run took 4.86 times longer than the fastest. This could mean that an intermediate result is being cached.
100 loops, best of 3: 2.36 ms per loop