2's complement conversion. Is this right?

Bob Greschke bob at passcal.nmt.edu
Mon Apr 21 19:06:39 EDT 2008


On 2008-04-21 16:51:13 -0600, Bob Greschke <bob at passcal.nmt.edu> said:
JUST COMPLETELY IGNORE THAT LAST ONE.  What a dope.  Here:

#! /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")[0]
    if Value > 0x800000:
        Value -= 0x1000000
system("date")
print

print "ord 1"
system("date")
for x in xrange(0, 100000000):
    Value = ord("a")
    if Value > 0x800000:
        Value -= 0x1000000
system("date")
print

print "unpack 3"
system("date")
for x in xrange(0, 100000000):
    Value1, Value2, Value3 = unpack(">BBB", "abc")
    Value = (Value1 << 16)+(Value2 << 8)+Value3
    if Value > 0x800000:
        Value -= 0x1000000
system("date")
print

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


Still, the differences between unpack and ord are significant (I just 
threw in the if's for fun).





More information about the Python-list mailing list