Any more room for optimization?

46 views
Skip to first unread message

Raphael C

unread,
Dec 23, 2016, 1:03:45 PM12/23/16
to cython...@googlegroups.com
I have the following code. cython -a tells me everything inside the
loop is now compiled to C.

Is there any further room for optimization using cython?

from __future__ import division
import numpy as np
cimport numpy as np
cimport cython


DTYPE_int = np.int
ctypedef np.int_t DTYPE_int_t
DTYPE_float = np.float64
ctypedef np.float64_t DTYPE_float_t

@cython.boundscheck(False) # turn off bounds-checking for entire function
@cython.wraparound(False) # turn off negative index wrapping for
entire function
def permfunc(np.ndarray [DTYPE_float_t, ndim =2] M):
cdef int n = M.shape[0]
cdef np.ndarray[DTYPE_float_t, ndim =1] d = np.ones(n, dtype=DTYPE_float)
cdef int j = 0
cdef int s = 1
cdef np.ndarray [DTYPE_int_t, ndim =1] f = np.arange(n, dtype=DTYPE_int)
cdef np.ndarray [DTYPE_float_t, ndim =1] v = M.sum(axis=0)
cdef DTYPE_float_t p = 1
cdef int i
cdef DTYPE_float_t prod
for i in range(n):
p *= v[i]
while (j < n-1):
for i in range(n):
v[i] -= 2*d[j]*M[j, i]
d[j] = -d[j]
s = -s
prod = 1
for i in range(n):
prod *= v[i]
p += s*prod
f[0] = 0
f[j] = f[j+1]
f[j+1] = j+1
j = f[0]
return p/2**(n-1)


Raphael

David Vierra

unread,
Dec 27, 2016, 10:04:01 PM12/27/16
to cython...@googlegroups.com
On 12/23/2016 7:51 AM, Raphael C wrote:
> I have the following code. cython -a tells me everything inside the
> loop is now compiled to C.
>
> Is there any further room for optimization using cython?
>
> cdef np.ndarray [DTYPE_int_t, ndim =1] f = np.arange(n, dtype=DTYPE_int)
> f[0] = 0
> f[j] = f[j+1]
> f[j+1] = j+1
> j = f[0]
>
>
> Raphael
>

I'm not sure I understand your algorithm completely, but it looks like
this "f" array could be replaced by a one or two integers?

--
David Vierra
MCEdit, a Minecraft World Editor
http://www.mcedit.net
http://twitter.com/codewarrior0

Robert Bradshaw

unread,
Dec 27, 2016, 11:53:58 PM12/27/16
to cython...@googlegroups.com
Also, if you use memory views, you can declare your arrays to be contiguous (C or Fortran style) and get rid of a dynamic slicing dimension multiplier (at the expense of less flexibility). 



--

--- 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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages