I am working on learning Cython (See How to Cythonize a Python Class with an attribute that is a function?)
I have the following function that I want to speed up, but Cython says both lines are going through python:
cdef selectindex(float fitnesspref):
r = np.random.rand()
return int(log(r) / log(fitnesspref))
So I need to get a random number (I originally just used random()
built in Python call, but switched to Numpy
hoping it was faster or would Cythonize better. Then I need to take the log of two floats, divide them, and then return the result back as an integer. Nothing too difficult, and should be a great question that people can then use later. I'm struggling to find simple solutions to all the info I need via the Cython
docs or Google. I would have thought this was like in every tutorial, but I'm not having a lot of luck.
Looking around though, I can't find an nice easy solution. For example: Canonical way to generate random numbers in Cython
Is there a best, but simpler, way to Cythonize a function like this?
As a side note, I had a similar problem with abs()
but then after I changed everything to be cdefs it seemed to automatically switch to using the C version.
Here is the results of the HTML generated by Cython:
Another common one I keep bumping into is:
start = time.time()
i.e. is there a simple way to get start and end times using C code to speed that up? I have that line there in an inner loop, so it's slowing things down. But it's really important to what I'm trying to do.
Update 1:
I'm trying to follow the suggestions in the comments and they don't seem to work out like I'd expect (which is why this all confuses me in the first place.) For example, here is a c version of random that I wrote and compiled. Why is it still yellow?
Update 2: Okay, I researched it further and here is why it won't compile all the way to C:
It's doing a check to make sure I'm not dividing by zero, even though it's a constant and can't ever be zero. How do you get rid of that?