Skip to content

Instantly share code, notes, and snippets.

@will-ca
will-ca / wrapper_cdef.pxd
Created March 26, 2020 06:04
Quick test of the speed difference between `object` and `PyObject*` for frequently re-created `cdef` attributes.
import cython
from cpython cimport array
from cpython.ref cimport PyObject
cdef class addend_ptrwrapper:
cdef PyObject* obj
cdef bint nonzero
@will-ca
will-ca / wrapper_cdef.py
Last active March 26, 2020 06:04
Quick test of the speed difference between `object` and `PyObject*` for frequently re-created `cdef` attributes.
# cython: language_level=3
import cython, random, array, timeit
test_floats = [*range(10000)]
test_tuples = [(i,) for i in range(10000)]
test_switches = array.array('i', [random.randint(0,1) for i in range(10000)])
# distutils: language = c++
import cython
def no_overhead(var):
# ~55ns
return 0
def py_overhead(var):
# ~60ns
if isinstance(var, float):
# distutils: language = c++
import cython
ctypedef fused fused_type_ohtest:
double
int
ctypedef fused fused_type_ohtest2:
double
int