cython

Cythonized function unexpectedly slow

…衆ロ難τιáo~ 提交于 2020-01-04 09:35:23
问题 I wanted to speed up a function that I'm using a lot and I though about using cython. However, after trying all the possible cython optimizations that I've been able to find in the documentation, the cython code is about 6 times slower than the python+numpy function. Disappointing! This is my test code: (forward1 is the python function, forward2 is the cython function) #geometry.py def forward1(points, rotation, translation): '''points are in columns''' return np.dot(rotation, points -

Cythonized function unexpectedly slow

三世轮回 提交于 2020-01-04 09:32:50
问题 I wanted to speed up a function that I'm using a lot and I though about using cython. However, after trying all the possible cython optimizations that I've been able to find in the documentation, the cython code is about 6 times slower than the python+numpy function. Disappointing! This is my test code: (forward1 is the python function, forward2 is the cython function) #geometry.py def forward1(points, rotation, translation): '''points are in columns''' return np.dot(rotation, points -

Cythonized function unexpectedly slow

为君一笑 提交于 2020-01-04 09:32:02
问题 I wanted to speed up a function that I'm using a lot and I though about using cython. However, after trying all the possible cython optimizations that I've been able to find in the documentation, the cython code is about 6 times slower than the python+numpy function. Disappointing! This is my test code: (forward1 is the python function, forward2 is the cython function) #geometry.py def forward1(points, rotation, translation): '''points are in columns''' return np.dot(rotation, points -

Pass a custom comparer to a priority queue in Cython

Deadly 提交于 2020-01-04 08:05:12
问题 The Cython libcpp module contains a template for priority_queue , which is great, except for one thing: I cannot pass it a custom comparer (or, at least, I don't know how to). I need this because I need the priority_queue to do an argsort of sorts rather than a sort (yes, a priority queue is optimal for what I want to do), and I need it to be fast. Is this possible within Cython, perhaps by wrapping a queue in a custom way, or not at all? As an example, say I want to sort a vector[int[:]] by

How to speed up numpy code

徘徊边缘 提交于 2020-01-03 19:00:08
问题 I have the following code. In principle it takes 2^6 * 1000 = 64000 iterations which is quite a small number. However it takes 9s on my computer and I would like to run it for n = 15 at least. from __future__ import division import numpy as np import itertools n=6 iters = 1000 firstzero = 0 bothzero = 0 for S in itertools.product([-1,1], repeat = n+1): for i in xrange(iters): F = np.random.choice(np.array([-1,0,0,1], dtype=np.int8), size = n) while np.all(F ==0): F = np.random.choice(np.array

Wrapping a pre-initialized pointer in a cython class

╄→尐↘猪︶ㄣ 提交于 2020-01-03 15:31:07
问题 I'm trying to use a C library which uses a callback function (callback_function) to provide a pointer to a struct I'd like to wrap (glp_tree). What is the correct way to initialize an instance with a pointer not created in __cinit__ ? I can't find an example of this pattern in the cython documentation. I have some working code (see below), which casts the pointer to an integer and back, but I'm not sure this is good practice / sane. cdef extern from "stdint.h": ctypedef unsigned long long

Propagating exceptions through dlsym cython

烂漫一生 提交于 2020-01-02 15:06:56
问题 I am unable to propagate exceptions through dlsym. I use dlsym to load a cythonized python file. I made a minimal working example below so you can try it yourself: I have a pyx file, c_fun.pyx, which I compile to a C file using Cython. Then I'm using dlsym to load in the so file in another program, say use_fun.c++. You can use ./compile.sh to compile the files. On executing ./test, the program crashes with a segmentation fault. #c_fun.pyx: cdef public double _myfunction(double x) except*: a=1

Cython: How to expose void* and function pointer in struct?

六眼飞鱼酱① 提交于 2020-01-02 13:28:06
问题 I have a C header with: typedef struct { <normal members> void (*cb_func)(glp_tree *T, void *info); void *cb_info; <normal members> } glp_iocp; Currently, in my pxd file: ctypedef struct IntOptCP "glp_iocp": <normal members> int out_dly # mip.out_dly (milliseconds) #void (*cb_func)(Tree* tree, void* info) # mip.cb_func #void* cb_info # mip.cb_info <normal members> In a pyx file, at some point, I do (essentially): cdef class MyClass: IntOptCP _iocp <__cintit__ and the like> def some_method

Cython: How to expose void* and function pointer in struct?

谁说胖子不能爱 提交于 2020-01-02 13:27:05
问题 I have a C header with: typedef struct { <normal members> void (*cb_func)(glp_tree *T, void *info); void *cb_info; <normal members> } glp_iocp; Currently, in my pxd file: ctypedef struct IntOptCP "glp_iocp": <normal members> int out_dly # mip.out_dly (milliseconds) #void (*cb_func)(Tree* tree, void* info) # mip.cb_func #void* cb_info # mip.cb_info <normal members> In a pyx file, at some point, I do (essentially): cdef class MyClass: IntOptCP _iocp <__cintit__ and the like> def some_method

Cython : exposing C++ classes with nested typedef (s)

末鹿安然 提交于 2020-01-02 09:28:51
问题 According to this question/answer in stackoverflow, it is not possible to directly rewrite C++ nested typedefs in cython. I have such a problem and I don't know which is the right/optimal way to proceed. Let me be more specific with an example. Below, you can find the content of two C++ files (one header.h and one .cpp) and of two corresponding cython files (one .pxd and one .pyx). In the C++ header file called cpp_graph.h you can see nested typedef declarations; for example, that