float32 to float64 performance best practices

77 views
Skip to first unread message

Andrea Gavana

unread,
Mar 14, 2021, 9:30:27 AM3/14/21
to cython...@googlegroups.com
Dear Cython users,

I am a very ignorant Cython user - and even weaker in terms of C skills, so I apologize in advance for the dumb questions.

I have a little Cython wrapper for a library written in C: this library expects, amongst other things, a 2D array of type double (float64), but for reasons long enough to explain, I have as input a 2D array of type float (float32) - and it’s also in Fortran order and the order should be preserved.

I know that I could simply and blindly do something like this in numpy:

double_array = float_array.astype(np.float64)

And pass that to my Cython wrapper. However, for matrices big enough, this is not exactly super performant. Does anyone have any suggestions on how to make that step in Cython or C potentially a bit faster? 

Thank you in advance for your help.

Andrea.


da-woods

unread,
Mar 14, 2021, 4:10:55 PM3/14/21
to cython...@googlegroups.com
I suspect the Numpy type conversion is actually pretty efficient. You may struggle to improve it all that much. You could try allocating the memory using "malloc" and then manually doing the loops yourself, but I'd be surprised if it made much difference (especially for large arrays)
--

---
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/cython-users/CAEf70bw0FHywC%3DjqG%3DL2mA_n-z1t1rhPMRSgHTUW8epNKweBgg%40mail.gmail.com.


Reply all
Reply to author
Forward
0 new messages