Code:Option Explicit Private Type WIN32_FIND_DATA 'Modified UDT dwFileAttributes As Long ftCreationTime As Currency ftLastAccessTime As Currency ftLastWriteTime As Currency nFileSize As Currency dwReserved As Currency cFileName(519) As Byte cAlternate(27) As Byte End Type Private Declare Function FindClose Lib "kernel32.dll" (ByVal hFindFile As Long) As Long Private Declare Function FindFirstFileW Lib "kernel32.dll" (ByVal lpFileName As Long, ByRef lpFindFileData As Any) As Long Private Declare Function QueryPerformanceCounter Lib "kernel32.dll" (ByRef lpPerformanceCount As Currency) As Long Private Declare Function QueryPerformanceFrequency Lib "kernel32.dll" (ByRef lpFrequency As Currency) As Long Private Declare Sub Sleep Lib "kernel32.dll" (Optional ByVal dwMilliseconds As Long) Private Sub Main() Const FILE1 = "C:\\\\\Windows\/\/\System32/////msvbvm60.dll" Const FILE2 = "C:\\\\\Windows\/\/\System32/////msvbvm70.dll" Dim RV As Boolean, i As Long, Iterations As Long, sRetVal As String Dim StartAPI As Currency, StopAPI As Currency, Freq As Currency Dim StartDIR As Currency, StopDIR As Currency MsgBox "FILE1 = """ & FILE1 & """" & vbNewLine & _ "FileExistsAPI(FILE1) = " & FileExistsAPI(FILE1) & vbNewLine & _ "FileExistsDIR(FILE1) = " & FileExistsDIR(FILE1) & vbNewLine & vbNewLine & _ "FILE2 = """ & FILE2 & """" & vbNewLine & _ "FileExistsAPI(FILE2) = " & FileExistsAPI(FILE2) & vbNewLine & _ "FileExistsDIR(FILE2) = " & FileExistsDIR(FILE2), vbInformation, "FileExists" Do: sRetVal = InputBox("Loop how many times?", "FileExistsAPI vs FileExistsDIR", "500,000") If StrPtr(sRetVal) = 0& Then Exit Sub ' Abort benchmark if canceled On Error Resume Next Iterations = CLng(sRetVal) - 1& Loop While Err If QueryPerformanceFrequency(Freq) Then Sleep QueryPerformanceCounter StartAPI For i = 0& To Iterations RV = FileExistsAPI(FILE1) 'Existing RV = FileExistsAPI(FILE2) 'Missing Next QueryPerformanceCounter StopAPI Sleep QueryPerformanceCounter StartDIR For i = 0& To Iterations RV = FileExistsDIR(FILE1) 'Existing RV = FileExistsDIR(FILE2) 'Missing Next QueryPerformanceCounter StopDIR MsgBox FormatNumber(Iterations + 1&, 0&) & " Iterations" & vbNewLine & vbNewLine & _ "API = " & Round((StopAPI - StartAPI) / Freq, 3&) & " secs" & vbNewLine & _ "DIR = " & Round((StopDIR - StartDIR) / Freq, 3&) & " secs", vbInformation, _ "FileExistsAPI vs FileExistsDIR" End If End Sub Public Function FileExistsAPI(ByRef sFileName As String) As Boolean Dim WFD As WIN32_FIND_DATA If LenB(sFileName) Then _ If FindClose(FindFirstFileW(StrPtr(sFileName), WFD)) Then _ FileExistsAPI = (WFD.dwFileAttributes And vbDirectory) <> vbDirectory End Function Public Function FileExistsDIR(ByRef sFileName As String) As Boolean Const FILE_ATTRIBS = vbArchive Or vbHidden Or vbReadOnly Or vbSystem If LenB(sFileName) Then FileExistsDIR = LenB(Dir(sFileName, FILE_ATTRIBS)) <> 0& End FunctionCode:Vista 500,000x ----------------- API = 75.787 secs DIR = 88.540 secs Win 7 500,000x ----------------- API = 86.682 secs DIR = 99.917 secs
Verdict: API rules!
Exe optimized for Fast Code with all Advanced Optimizations on. Both benchmarks performed in Safe Mode with Command Prompt. Process boosted to Realtime priority. Tested with both Vista and Win 7 on the same machine.





Reply With Quote