最快的版本,没有使用以下代码过度优化我的代码:
class CTError(Exception): def __init__(self, errors): self.errors = errorstry: O_BINARY = os.O_BINARYexcept: O_BINARY = 0READ_FLAGS = os.O_RDonLY | O_BINARYWRITE_FLAGS = os.O_WRonLY | os.O_CREAT | os.O_TRUNC | O_BINARYBUFFER_SIZE = 128*1024def copyfile(src, dst): try: fin = os.open(src, READ_FLAGS) stat = os.fstat(fin) fout = os.open(dst, WRITE_FLAGS, stat.st_mode) for x in iter(lambda: os.read(fin, BUFFER_SIZE), ""): os.write(fout, x) finally: try: os.close(fin) except: pass try: os.close(fout) except: passdef copytree(src, dst, symlinks=False, ignore=[]): names = os.listdir(src) if not os.path.exists(dst): os.makedirs(dst) errors = [] for name in names: if name in ignore: continue srcname = os.path.join(src, name) dstname = os.path.join(dst, name) try: if symlinks and os.path.islink(srcname): linkto = os.readlink(srcname) os.symlink(linkto, dstname) elif os.path.isdir(srcname): copytree(srcname, dstname, symlinks, ignore) else: copyfile(srcname, dstname) # XXX What about devices, sockets etc.? except (IOError, os.error), why: errors.append((srcname, dstname, str(why))) except CTError, err: errors.extend(err.errors) if errors: raise CTError(errors)
此代码的运行速度比本地linux“ cp -rf”慢。
比较本地存储到tmfps的收益大约是2倍至3倍,而NFS到本地存储则是6倍左右。
分析后,我注意到shutil.copy做了很多非常重要的fstat
syscal。如果要进一步优化,我建议对src做一个fstat并重用这些值。老实说,我没有走更远的一步,因为我得到的数字几乎与本地linux复制工具相同,而优化几百毫秒并不是我的目标。



