gpt4 book ai didi

Windows x64 上的 Python x64 位复制文件性能评估/问题

转载 作者:可可西里 更新时间:2023-11-01 13:29:04 28 4
gpt4 key购买 nike

在编写一种备份应用程序时,我对 Windows 上的文件复制性能进行了评估。

我有几个问题,想知道您的意见。

谢谢!

卢卡斯。

问题:

  1. 与复制 1 GiB 文件相比,为什么复制 10 GiB 文件的性能要慢得多?

  2. 为什么 shutil.copyfile 这么慢?

  3. 为什么 win32file.CopyFileEx 这么慢?这可能是因为标志 win32file.COPY_FILE_RESTARTABLE?但是,它不接受 int 1000 作为标志(COPY_FILE_NO_BUFFERING),建议用于大文件: http://msdn.microsoft.com/en-us/library/aa363852%28VS.85%29.aspx

  4. 使用空的 ProgressRoutine 似乎对完全不使用 ProgressRoutine 没有任何影响。

  5. 是否有另一种性能更好的方式来复制文件并获取进度更新?

1 GiB 和 10 GiB 文件的结果:

test_file_size             1082.1 MiB    10216.7 MiB

METHOD SPEED SPEED
robocopy.exe 111.0 MiB/s 75.4 MiB/s
cmd.exe /c copy 95.5 MiB/s 60.5 MiB/s
shutil.copyfile 51.0 MiB/s 29.4 MiB/s
win32api.CopyFile 104.8 MiB/s 74.2 MiB/s
win32file.CopyFile 108.2 MiB/s 73.4 MiB/s
win32file.CopyFileEx A 14.0 MiB/s 13.8 MiB/s
win32file.CopyFileEx B 14.6 MiB/s 14.9 MiB/s

测试环境:

Python:
ActivePython 2.7.0.2 (ActiveState Software Inc.) based on
Python 2.7 (r27:82500, Aug 23 2010, 17:17:51) [MSC v.1500 64 bit (AMD64)] on win32

source = mounted network drive
source_os = Windows Server 2008 x64

destination = local drive
destination_os = Windows Server 2008 R2 x64

注意事项:

'robocopy.exe' and 'cmd.exe /c copy' were run using subprocess.call()

win32file.CopyFileEx A(不使用 ProgressRoutine):

def Win32_CopyFileEx_NoProgress( ExistingFileName, NewFileName):
win32file.CopyFileEx(
ExistingFileName, # PyUNICODE | File to be copied
NewFileName, # PyUNICODE | Place to which it will be copied
None, # CopyProgressRoutine | A python function that receives progress updates, can be None
Data = None, # object | An arbitrary object to be passed to the callback function
Cancel = False, # boolean | Pass True to cancel a restartable copy that was previously interrupted
CopyFlags = win32file.COPY_FILE_RESTARTABLE, # int | Combination of COPY_FILE_* flags
Transaction = None # PyHANDLE | Handle to a transaction as returned by win32transaction::CreateTransaction
)

win32file.CopyFileEx B(使用空的 ProgressRoutine):

def Win32_CopyFileEx( ExistingFileName, NewFileName):
win32file.CopyFileEx(
ExistingFileName, # PyUNICODE | File to be copied
NewFileName, # PyUNICODE | Place to which it will be copied
Win32_CopyFileEx_ProgressRoutine, # CopyProgressRoutine | A python function that receives progress updates, can be None
Data = None, # object | An arbitrary object to be passed to the callback function
Cancel = False, # boolean | Pass True to cancel a restartable copy that was previously interrupted
CopyFlags = win32file.COPY_FILE_RESTARTABLE, # int | Combination of COPY_FILE_* flags
Transaction = None # PyHANDLE | Handle to a transaction as returned by win32transaction::CreateTransaction
)

def Win32_CopyFileEx_ProgressRoutine(
TotalFileSize,
TotalBytesTransferred,
StreamSize,
StreamBytesTransferred,
StreamNumber,
CallbackReason, # CALLBACK_CHUNK_FINISHED or CALLBACK_STREAM_SWITCH
SourceFile,
DestinationFile,
Data): # Description
return win32file.PROGRESS_CONTINUE # return of any win32file.PROGRESS_* constant

最佳答案

问题三:

您误解了 Microsoft API 中的 COPY_FILE_NO_BUFFERING 标志。它不是 int 1000,而是十六进制 1000(0x1000 => int 值:4096)。当您设置 CopyFlags = 4096 时,您将拥有 Windows 环境中最快的(?)复制例程。我在我的数据备份代码中使用相同的例程,该例程速度非常快,并且每天传输 TB 大小的数据。

问题4:

没关系,因为它是一个回调。但总的来说,你不应该在里面放太多代码并保持干净和光滑。

问题5:

根据我的经验,这是标准 Windows 环境中最快的复制例程。可能有更快的自定义复制例程,但是当使用普通的 Windows API 时,找不到更好的了。

关于Windows x64 上的 Python x64 位复制文件性能评估/问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4279207/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com