#!/usr/local/bin/python
from md5 import md5
import string
from sys import stdin
h = string.hexdigits
def hexstr(s):
r = ''
for c in s:
i = ord(c)
r = r + h[(i >> 4) & 0xF] + h[i & 0xF]
return r
bufsiz = 8192
buff = stdin.read(bufsiz)
m = md5()
while (buff):
m.update(buff)
buff = stdin.read(bufsiz)
print hexstr(m.digest())
Here are the results, run over 18,903,040 bytes of data:
Python: 27.36 seconds
C: 33.06 seconds
Now it's obvious that at someone didn't try hard in the C version; rooting
through the code I find the offending line:
while (len = fread (buffer, 1, 16, stdin))
However, I still think this stands as useful evidence that Python is a
reasonably efficient language, quite suitable for getting real work done.
-Bennett
bet@sbi.com
P.S. Here's the typescript of my benchmark run:
; tar cf - archive | /usr/local/bin/time -v md5;tar cf - archive | /usr/local/bin/time -v md5.py
a7c730d346cbf7d30d370e84cf1698ae
Summary statistics:
Command being timed: "md5"
User time (seconds): 23.77
System time (seconds): 2.68
Percent of CPU this job got: 79%
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:33.06
Average shared text size (kbytes): 0
Average unshared data size (kbytes): 375
Average stack size (kbytes): 0
Average total size (kbytes): 375
Maximum resident set size (kbytes): 196
Average resident set size (kbytes): 375
Major (requiring I/O) page faults: 0
Minor (reclaiming a frame) page faults: 85
Voluntary context switches: 6
Involuntary context switches: 5878
Swaps: 0
File system inputs: 0
File system outputs: 0
Socket messages sent: 0
Socket messages received: 5538
Signals delivered: 0
Page size (bytes): 4096
Exit status: 0
a7c730d346cbf7d30d370e84cf1698ae
Summary statistics:
Command being timed: "md5.py"
User time (seconds): 17.26
System time (seconds): 3.9
Percent of CPU this job got: 74%
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:27.36
Average shared text size (kbytes): 0
Average unshared data size (kbytes): 1544
Average stack size (kbytes): 0
Average total size (kbytes): 1544
Maximum resident set size (kbytes): 784
Average resident set size (kbytes): 1544
Major (requiring I/O) page faults: 0
Minor (reclaiming a frame) page faults: 172
Voluntary context switches: 12
Involuntary context switches: 5783
Swaps: 0
File system inputs: 0
File system outputs: 0
Socket messages sent: 0
Socket messages received: 5538
Signals delivered: 0
Page size (bytes): 4096
Exit status: 0
;