Commit eabb0131 authored by Jeremy Hylton's avatar Jeremy Hylton

Take two: Speed up p64(), u64(), and U64().

Also, reformat code using guidlines from the Friends of Whitespace.

Fix newTimeStamp() so that it always returns a timestamp.
XXX still not sure that it works.
parent fd165358
...@@ -89,37 +89,41 @@ t32 = 1L << 32 ...@@ -89,37 +89,41 @@ t32 = 1L << 32
def p64(v, pack=struct.pack): def p64(v, pack=struct.pack):
"""Pack an integer or long into a 8-byte string""" """Pack an integer or long into a 8-byte string"""
if v < t32: h=0 if v < t32:
h = 0
else: else:
h=v/t32 h, v = divmod(v, t32)
v=v%t32
return pack(">II", h, v) return pack(">II", h, v)
def u64(v, unpack=struct.unpack): def u64(v, unpack=struct.unpack):
"""Unpack an 8-byte string into a 64-bit (or long) integer""" """Unpack an 8-byte string into a 64-bit (or long) integer"""
h, v = unpack(">ii", v) h, v = unpack(">ii", v)
if v < 0: v=t32+v if v < 0:
v = t32 + v
if h: if h:
if h < 0: h=t32+h if h < 0:
v=h*t32+v h= t32 + h
v = (long(h) << 32) + v
return v return v
def U64(v, unpack=struct.unpack): def U64(v, unpack=struct.unpack):
"""Same as u64 but always returns a long.""" """Same as u64 but always returns a long."""
h, v = unpack(">II", v) h, v = unpack(">II", v)
if h: if h:
v=h*t32+v v = (long(h) << 32) + v
return v return v
def cp(f1, f2, l): def cp(f1, f2, l):
read=f1.read read = f1.read
write=f2.write write = f2.write
n=8192 n =8192
while l > 0: while l > 0:
if n > l: n=l if n > l:
d=read(n) n = l
if not d: break d = read(n)
if not d:
break
write(d) write(d)
l = l - len(d) l = l - len(d)
...@@ -127,8 +131,8 @@ def cp(f1, f2, l): ...@@ -127,8 +131,8 @@ def cp(f1, f2, l):
def newTimeStamp(old=None, def newTimeStamp(old=None,
TimeStamp=TimeStamp.TimeStamp, TimeStamp=TimeStamp.TimeStamp,
time=time.time, gmtime=time.gmtime): time=time.time, gmtime=time.gmtime):
t=time() t = time()
ts=TimeStamp(gmtime(t)[:5]+(t%60,)) ts = TimeStamp(gmtime(t)[:5]+(t%60,))
if old is not None: return ts.laterThan(than) if old is not None:
return ts.laterThan(old)
return ts
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment