Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import timeit
from django.conf import settings; settings.configure()
from django.dispatch import Signal
def handle(sender, **kw):
pass
handlers10 = [lambda **kw: None for i in range(10)]
handlers100 = [lambda **kw: None for i in range(100)]
def func_call_signal():
handle(None)
my_sig = Signal()
do_nothing = timeit.Timer('pass')
func_call = timeit.Timer('func_call_signal()', 'from __main__ import func_call_signal')
signal_0 = timeit.Timer('my_sig.send(None)', 'from __main__ import my_sig')
signal_1 = timeit.Timer('my_sig.send(None)', 'from __main__ import my_sig, handle\nmy_sig.connect(handle)')
signal_10 = timeit.Timer('my_sig.send(None)', 'from __main__ import my_sig, handlers10\nfor h in handlers10: my_sig.connect(h)')
signal_100 = timeit.Timer('my_sig.send(None)', 'from __main__ import my_sig, handlers100\nfor h in handlers100: my_sig.connect(h)')
TIMES = 100000
t_nothing = do_nothing.timeit(TIMES)
t_func = func_call.timeit(TIMES)
t_s0 = signal_0.timeit(TIMES)
t_s1 = signal_1.timeit(TIMES)
t_s10 = signal_10.timeit(TIMES)
t_s100 = signal_100.timeit(TIMES)
print "Nothing : %0.5f (%0.9f percall)" % (t_nothing, t_nothing/TIMES)
print "Plain func call : %0.5f (%0.9f percall)" % (t_func, t_func/TIMES)
print "Signal; 0 handlers: %0.5f (%0.9f percall)" % (t_s0, t_s0/TIMES)
print "Signal; 1 handler : %0.5f (%0.9f percall)" % (t_s1, t_s1/TIMES)
print "Signal; 10 handlers: %0.5f (%0.9f percall)" % (t_s10, t_s10/TIMES)
print "Signal; 100 handlers: %0.5f (%0.9f percall)" % (t_s100, t_s100/TIMES)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.