为什么模拟的 Parallel 函数在第一个 运行 上执行缓慢但随后加速?

Why impersonated Parallel function performs slow on the first run but speeds up subsequently?

所以我有一些代码可以将文件复制到网络上的 5 台远程 PC。我有一个 class 模拟需要登录的方法

''' <summary>
''' Returns the names of the files including their path that match the regex pattern found in this directory. 
''' </summary>
''' <param name="searchOption">
''' The parameter option SearchOption.AllDirectories indicates that the search should include the current directory and any subdirectories
''' The parameter option SearchOption.TopDirectoryOnly indicates that the search should only include the current directory. 
''' </param> 
Public Function GetFileContent(filePath As String, searchPattern As String, Optional searchOption As SearchOption = Nothing) As String() Implements IEPMADirectoryAccess.GetFileContent
    If searchOption = Nothing Then
        searchOption = SearchOption.TopDirectoryOnly
    End If

    LogonUser(UserName, Domain, Password, LogonType.LOGON32_LOGON_NEW_CREDENTIALS, 0, UserToken)
    Using WindowsIdentity.Impersonate(UserToken)
        Return Directory.GetFiles(filePath, searchPattern, searchOption)
    End Using
End Function
''' <summary>
''' Get the name of the Ward from the directory of the current file path 
''' </summary>
Public Function GetWardName(filePath As String) As String Implements IEPMADirectoryAccess.GetWardName
    LogonUser(UserName, Domain, Password, LogonType.LOGON32_LOGON_NEW_CREDENTIALS, 0, UserToken)
    Using WindowsIdentity.Impersonate(UserToken)
        Return New DirectoryInfo(Path.GetDirectoryName(filePath)).Name
    End Using
End Function
''' <summary>
''' Creates a directory for the given path 
''' </summary>
Public Sub CreateDirectory(directoryPath As String) Implements IEPMADirectoryAccess.CreateDirectory
    LogonUser(UserName, Domain, Password, LogonType.LOGON32_LOGON_NEW_CREDENTIALS, 0, UserToken)
    Using WindowsIdentity.Impersonate(UserToken)
        Directory.CreateDirectory(directoryPath)
    End Using
End Sub
''' <summary>
''' Deletes a file from the specified path
''' </summary>
Public Sub DeleteFile(filePath As String) Implements IEPMADirectoryAccess.DeleteFile
    LogonUser(UserName, Domain, Password, LogonType.LOGON32_LOGON_NEW_CREDENTIALS, 0, UserToken)
    Using WindowsIdentity.Impersonate(UserToken)
        File.Delete(filePath)
    End Using
End Sub
''' <summary>
''' Copies a file from the source path to the destination path 
''' </summary>
Public Sub CopyFile(sourceFilePath As String, destFilePath As String, overwrite As Boolean) Implements IEPMADirectoryAccess.CopyFile
    LogonUser(UserName, Domain, Password, LogonType.LOGON32_LOGON_NEW_CREDENTIALS, 0, UserToken)
    Using WindowsIdentity.Impersonate(UserToken)
        File.Copy(sourceFilePath, destFilePath, overwrite)
    End Using
End Sub

并行循环中的部分代码如下所示:

                                                        'If PC is switched on 
                                                         If IsOnline(device.PCName) Then

                                                             'For each PC of the ward, get the corresponding folder path
                                                             targetPath = "\" + device.PCName + "\c$Report P\"

                                                             'Requires access to the network share 
                                                             If Not Directory.Exists(targetPath) Then
                                                                 DirectoryAccess.CreateDirectory(targetPath)
                                                             End If

                                                             'Requires access to the network share 
                                                             For Each fileName As String In DirectoryAccess.GetFileContent(targetPath, "*.pdf")
                                                                 'Purge (Delete) previous versions of this file in the destination folder  
                                                                 Dim fileConst As String

                                                                 fileConst = arr(1)
                                                                 If fileName.Contains(fileConst) Then
                                                                     'Requires access to the network share 
                                                                     DirectoryAccess.DeleteFile(fileName)
                                                                 End If
                                                             Next

                                                             DirectoryAccess.CopyFile(f, Path.Combine(targetPath, Path.GetFileName(f), True)


                                                         End If

在主模块中,我计算了使用 普通 for 循环与并行 for 循环相比,但 for 循环内的代码内容相同:

enter image description here

最初,并行复制为 18 秒,但随后变为 2.82 秒 运行。

enter image description here

第三个运行是1.81秒。

我已检查用于模拟的 using 块已正确处理并在退出 using 块后恢复为我的原始凭据。我不确定为什么它在第一个 运行 时很慢,但后来却加快了?会不会是一开始这四台 PC 都处于非活动状态,所以复制过程激活了它们但需要更长时间?

我有一个 IsOnline 功能可以检查电脑是否开机,如果没有我就跳过它们以确保它不会减慢进程。

enter image description here

原来它很慢,因为时间是在不同数量的电脑上测量的。一些 PC 在测量期间进入睡眠状态,因此影响了处理时间