Skip to content

Instantly share code, notes, and snippets.

@bra-fsn
Last active June 3, 2016 07:05
Show Gist options
  • Save bra-fsn/1fd481b44590a939e849cb9073ba1a41 to your computer and use it in GitHub Desktop.
Save bra-fsn/1fd481b44590a939e849cb9073ba1a41 to your computer and use it in GitHub Desktop.
defertothread benchmark
from twisted.internet import reactor
from twisted.internet.threads import deferToThread
from twisted.internet.defer import inlineCallbacks
from time import time
import sys
def func(a):
return a
@inlineCallbacks
def dtt():
t1=0
t2=0
for i in xrange(10000):
st=time()
res = yield deferToThread(func,i)
t1+=time()-st
st=time()
res = func(i)
t2+=time()-st
print ("deferToThread: avg %.2f us, sync: avg %.2f us, %.2fx increase" % (t1/i*1000**2, t2/i*1000**2, (t1/i)/(t2/i)))
reactor.callWhenRunning(dtt)
reactor.suggestThreadPoolSize(64)
for i in range(int(sys.argv[1])):
reactor.callInThread(dtt)
reactor.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment