【问题标题】:How can I make this recursive function more efficient?我怎样才能使这个递归函数更有效?
【发布时间】:2016-03-07 04:54:36
【问题描述】:

我创建了这个函数来递归地从 FTP 服务器复制整个目录。它工作得很好,只是它比使用 FileZilla 执行相同的操作慢了大约 4 倍。下载 FileZilla 中的目录大约需要 55 秒,但使用此功能需要 229 秒。我该怎么做才能使其下载/运行更快?

Private Sub CopyEntireDirectory(ByVal directory As String)
    Dim localPath = localDirectory & formatPath(directory)
    'creates directory in destination path
    IO.Directory.CreateDirectory(localPath)

    'Gets the directory details so I can separate folders from files
    Dim fileList As ArrayList = Ftp.ListDirectoryDetails(directory, "")

    For Each item In fileList
        'checks if it's a folder or file: d=folder
        If (item.ToString().StartsWith("d")) Then
            'gets the directory from the details
            Dim subDirectory As String = item.ToString().Substring(item.ToString().LastIndexOf(" ") + 1)
            CopyEntireDirectory(directory & "/" & subDirectory)
        Else
            Dim remoteFilePath As String = directory & "/" & item.ToString().Substring(item.ToString().LastIndexOf(" ") + 1)
            Dim destinationPath = localPath & "\" & item.ToString().Substring(item.ToString().LastIndexOf(" ") + 1)
            'downloads file to destination directory
            Ftp.DownLoadFile(remoteFilePath, destinationPath)
        End If
    Next
End Sub

下面是一直占用的下载功能。

Public Sub DownLoadFile(ByVal fromFilename As String, ByVal toFilename As String)
    Dim files As ArrayList = Me.ListDirectory(fromFilename, "")
    Dim request As FtpWebRequest = Me.CreateRequestObject(fromFilename)
    request.Method = WebRequestMethods.Ftp.DownloadFile

    Dim response As FtpWebResponse = CType(request.GetResponse(), FtpWebResponse)
    If response.StatusCode <> FtpStatusCode.OpeningData AndAlso response.StatusCode <> FtpStatusCode.DataAlreadyOpen Then
        Throw New ApplicationException(Me.BuildCustomFtpErrorMessage(request, response))
    End If

    Dim fromFilenameStream As Stream = response.GetResponseStream()
    Dim toFilenameStream As FileStream = File.Create(toFilename)

    Dim buffer(BLOCK_SIZE) As Byte
    Dim bytesRead As Integer = fromFilenameStream.Read(buffer, 0, buffer.Length)
    Do While bytesRead > 0
        toFilenameStream.Write(buffer, 0, bytesRead)
        Array.Clear(buffer, 0, buffer.Length)
        bytesRead = fromFilenameStream.Read(buffer, 0, buffer.Length)
    Loop

    response.Close()
    fromFilenameStream.Close()
    toFilenameStream.Close()
End Sub

【问题讨论】:

  • 还没有人指责 VB 速度快/效率高...
  • 它应该是 I/O 绑定的。暂停几次,看看 80% 的时间都在做什么。
  • @MarcB -- 啊,是的,又是对 VB 的厌倦挖掘。我已经使用 VB 和 C# 有一段时间了,我没有发现速度或效率差异。
  • 这个问题可能更适合 Code Review 网站。 codereview.stackexchange.com
  • 哇,谢谢,不知道有没有!

标签: vb.net performance recursion ftp-client


【解决方案1】:

缓慢显然是在 FTP 命令中。以递归方式运行您的其他代码可能每秒运行一百万次,因为它没有任何内容。

FTP 下载(无论它是什么)应该能够定义被抓取的块的大小。这将是您速度的关键。它需要根据您的连接速度和文件大小进行优化。没有适合每个人的号码。

编辑

根据新代码,问题出在您的BLOCK_SIZE 中,我认为这是一个常数。玩这个大小以获得最佳速度。

提示:这应该是 1024 的倍数

【讨论】:

  • 我添加了下载功能,如果这有助于更好地解释事情。
猜你喜欢
  • 2014-01-07
  • 2014-01-05
  • 1970-01-01
  • 2021-10-06
  • 2010-10-16
  • 2017-03-23
  • 2010-11-14
  • 1970-01-01
  • 2020-04-06
相关资源
最近更新 更多