| Sign In/My Account | View Cart |
| Article: |
Untwisting Python Network Programming | |
| Subject: | Why untwisting? | |
| Date: | 2006-08-11 10:34:28 | |
| From: | glyph@divmod.com | |
|
Many of the examples in this article are wrong. I'll just point out the errors in the telnet example, because I'm more familiar with Twisted than non-Twisted code, and I also don't have too much time to spend reviewing it...
|
||
Showing messages 1 through 5 of 5.
Using core modules:
start server (no net): 0.5809 sec
send mails (smtp): 1.3601 sec
view mails (pop3): 0.7007 sec
delete mails (pop3): 0.5187 sec
stop server (telnet): 0.5124 sec
Using Twisted:
start server (no net): 0.5959 sec
send mails (smtp): 2.2488 sec
view mails (pop3): 1.4274 sec
delete mails (pop3): 1.3074 sec
stop server (telnet): 1.3213 sec
#!/usr/bin/python
# file: mail-timeit.py
# Measures the timing of invoking mail-core.py and mail-twisted.py
from time import sleep
from timeit import Timer
cmdline = ''
def doit(cmd, arg, array, rest):
global cmdline
cmdline = cmd + ' ' + arg
print; print cmdline
array.append(Timer('os.system(cmdline)',
'import os; from __main__ import cmdline').timeit(1))
sleep(rest)
def dostat(cmd, times):
stat = [[], [], [], [], []] # for 1, s, v, d, 0
for i in range(times):
doit(cmd, '1', stat[0], 12)
doit(cmd, 's', stat[1], 5)
doit(cmd, 'v', stat[2], 1)
doit(cmd, 'd', stat[3], 1)
doit(cmd, '0', stat[4], 2)
return stat
def avgstat(stat, fr, to):
return [ sum(i[fr:to]) / (to-fr) for i in stat ]
def printavgs(avgs):
labels = [
'start server (no net)',
'send mails (smtp)',
'view mails (pop3)',
'delete mails (pop3)',
'stop server (telnet)']
for i, j in zip(labels, avgs):
print '%25s: %.4f sec' % (i, j)
if __name__ == '__main__':
times = 11
stat1 = dostat('mail-core.py', times)
avgs1 = avgstat(stat1, 1, times)
stat2 = dostat('mail-twisted.py', times)
avgs2 = avgstat(stat2, 1, times)
# print stat1
printavgs(avgs1)
# print stat2
printavgs(avgs2)
# end of mail-timeit.py
Regarding performance, I observed the core modules runs faster than Twisted when running the programs. I also briefly did some measurements and here are the results:
These numbers are the average of 10 runs, and the mail server is run in localhost. While the measurements are by no means vigorous, they basically agree with the observations. Of course, the differences may not be significant in real uses when the network delay counts for majority of the execution time.