Last active
February 20, 2020 18:31
-
-
Save will-ca/a06203fe9b3c54ad8e05fede1e2cbb49 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# distutils: language = c++ | |
import cython | |
def no_overhead(var): | |
# ~55ns | |
return 0 | |
def py_overhead(var): | |
# ~60ns | |
if isinstance(var, float): | |
a = var | |
return 0 | |
elif isinstance(var, int): | |
b = var | |
return 0 | |
def fused_type_overhead(var): | |
# ~370ns | |
return 0 | |
def fused_bind(self, var): # Works. | |
return 0 | |
class fused_type_cls: | |
meth = fused_bind # Segfaults. | |
class compiled_fused_type_cls: | |
meth = fused_bind # Segfaults. | |
def static_meth(self, var): # Works. | |
return 0 | |
def fused_type_cls_compiledtest(): | |
obj = compiled_fused_type_cls() | |
v = 1.0 | |
return (obj.meth(v), obj.static_meth(v)) # Segfaults. | |
def fused_type_staticwrapped(var): | |
# ~65ns | |
if isinstance(var, float): | |
a = var | |
return fused_type_overhead(a) | |
elif isinstance(var, int): | |
b = var | |
return fused_type_overhead(b) | |
def fused_type_multisig(var1, var2): | |
return 0 | |
def bigfused_type_overhead(var): | |
return 0 | |
def bigfused_type_multisig(var1, var2, var3): | |
return 0 | |
def kwargonly_func(self, a, b={}): | |
if not b: | |
b.update(func=1) | |
return (self, a, b) | |
class kwarg_callable: | |
def __init__(self, callobj, boundobj=None): | |
self.callobj = callobj | |
self.boundobj = None | |
def __call__(self, a, b, *, c={}): | |
if not c: | |
c.update(call=1) | |
if self.boundobj is None: | |
return self.callobj(a, b) | |
else: | |
return self.callobj(self.boundobj, a, b) | |
def __get__(self, obj, cls=None): | |
if obj is None: | |
return type(self)(self.callobj, cls) | |
else: | |
return type(self)(self.callobj, obj) | |
kwarg_callableinstance = kwarg_callable(kwargonly_func) | |
class kwcls: | |
kwargonly_bound = kwargonly_func | |
kwargcall_bound = kwarg_callableinstance | |
def kwargonly_meth(self, a, *, b={}): | |
if not b: | |
b.update(meth=1) | |
return (self, a, b) | |
def run_tests(): | |
import timeit, sys | |
for n, a in (('no_overhead', 1), ('py_overhead', 1), ('fused_type_overhead', 1), ('fused_type_staticwrapped', 1), ('fused_type_multisig', 2), ('bigfused_type_overhead', 1), ('bigfused_type_multisig', 3)): | |
print('{n}:'.format(n=n)) | |
if sys.version_info.major < 3: | |
print(min(timeit.repeat(stmt='{n}({ar})'.format(n=n, ar=",".join("abcd"[:a])), setup='a,b,c,d=5.0,3,2.5,6.0; from fusedtest import {n}'.format(n=n), repeat=1000, number=1000))) | |
else: | |
print(min(timeit.repeat(stmt='{n}({ar})'.format(n=n, ar=",".join("abcd"[:a])), setup='a,b,c,d=5.0,3,2.5,6.0', globals=globals(), repeat=1000, number=1000))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment