2's complement conversion. Is this right?

Bob Greschke bob at passcal.nmt.edu
Tue Apr 22 00:51:13 CEST 2008


Something is fishy.  I just ran this simple-minded thing and I'm, 
again, getting better times for ord() than I am for unpack() on a 
2.8GHz OSX iMac with 2.5.1.  This is the iterate so many times you can 
use your wristwatch method:

----

#! /usr/bin/env python

from os import system
from struct import unpack

print "unpack 1"
system("date")
for x in xrange(0, 100000000):
    Value = unpack(">B", "a")
system("date")
print

print "ord 1"
system("date")
for x in xrange(0, 100000000):
    Value = ord("a")
system("date")
print

print "unpack 3"
system("date")
for x in xrange(0, 100000000):
    Value = unpack(">BBB", "abc")
system("date")
print

print "ord 3"
system("date")
for x in xrange(0, 100000000):
    Value = (ord("a") << 16)+(ord("b") << 8)+ord("c")
system("date")


----

Unpack 1 is about 1m20s
Ord 1 is about 20s
Unpack 3 is about 1m20s
Ord 3 is about 1m03s
after averaging a few runs.




More information about the Python-list mailing list