3

同じ API 呼び出しが、VB.NET よりも VB6 の方がはるかに速く返される理由を誰か説明できますか?

これが私のVB6コードです:

Public Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long


Public Function GetWindowTextEx(ByVal uHwnd As Long) As String

Dim lLen&
lLen = GetWindowTextLength(uHwnd) + 1

Dim sTemp$
sTemp = Space(lLen)

lLen = GetWindowText(uHwnd, sTemp, lLen)

Dim sRes$
sRes = Left(sTemp, lLen)

GetWindowTextEx = sRes

End Function

そして、ここに私のVB.NETコードがあります:

Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Integer, ByVal lpWindowText As String, ByVal cch As Integer) As Integer

    Dim sText As String = Space(Int16.MaxValue)
    GetWindowText(hwnd, sText, Int16.MaxValue)

各バージョンを 1000 回実行しました。

VB6 バージョンでは 2.04893359351538 ミリ秒が必要でした。VB.NET バージョンでは 372.1322491699365 ミリ秒が必要でした。

リリース バージョンとデバッグ バージョンはほぼ同じです。

ここで何が起きてるの?

4

1 に答える 1

7

*A バージョンを使用せず、接尾辞をスキップして、String の代わりに StringBuilder を使用します。

Private Declare Auto Function GetWindowText Lib "user32" (ByVal hwnd As Integer, ByVal lpWindowText As StringBuilder, ByVal cch As Integer) As Integer
Private Declare Function GetWindowTextLength Lib "user32" (ByVal hwnd As Integer) As Integer

Dim len As Integer = GetWindowTextLength (hwnd)
Dim str As StringBuilder = new StringBuilder (len)
GetWindowText (hwnd, str, str.Capacity)
于 2012-11-06T22:48:01.540 に答える