37,721
社区成员
发帖
与我相关
我的任务
分享
file=open(self.scroll.filename,"rb")
file.seek(0,0)
data=file.read(0xFFFFF)
dfile=open("tmp.bin","wb")
while data!="":
i=0
temp=""
while i<len(data):
str=data[i:i+2]
for ch in reversed(str):
temp+=ch
i+=2
dfile.write(temp)
data=file.read(0xFFFFF)
file.close()
dfile.close()
os.remove("test.bin")
os.rename("tmpqrj.bin", "test.bin")
for ch in reversed(str):
temp = temp.join(['',ch])
i+=2
for ch in reversed(str):
temp = '%s%s'%(temp,ch)
i+=2
t1 = clock()
swap_bytes('_', '_2')
t2 = clock()
print t2 - t1
t1 = clock()
swap_bytes_v2('_', '_2')
t2 = clock()
print t2 - t1
from time import clock
def swap_bytes(sourcepath, targetpath):
rf = open(sourcepath,"rb")
rf.seek(0,0)
data = rf.read(0xFFFFF)
wf = open(targetpath, "wb")
while data!="":
i=0
temp=""
while i<len(data):
str=data[i:i+2]
for ch in reversed(str):
temp+=ch
i+=2
wf.write(temp)
data=rf.read(0xFFFFF)
rf.close()
wf.close()
def swap_bytes_v2(sourcepath, targetpath):
from array import array
import os.path
byte_array = array('h')
rf = open(sourcepath, 'rb')
wf = open(targetpath, 'wb')
size = os.path.getsize(sourcepath) / 2
byte_array.fromfile(rf, size)
byte_array.byteswap()
byte_array.tofile(wf)
t1 = clock()
swap_bytes('_', '_2')
t2 = clock()
t1 = clock()
swap_bytes_v2('_', '_2')
t2 = clock()
print t2 - t1
while i<len(data):
str=data[i:i+2]
for ch in reversed(str):
temp+=ch
i+=2
while i<len(data):
temp+=data[i:i+2][::-1]
i+=2
file=open(self.filename,"rb")
file.seek(0,0)
data=file.read(0xFFFFF)
dfile=open("tmp.bin","wb")
while data!="":
i=0
temp=""
while i<len(data):
str=data[i:i+2]
for ch in reversed(str):
temp+=ch
i+=2
dfile.write(temp)
data=file.read(0xFFFFF)
file.close()
dfile.close()
os.remove("test.bin")
os.rename("tmp.bin", self.filename)