Re: Schnelles Zählen von Dateien
Verfasst: 23.11.2010 22:04
Die WinAPI-Variante (leicht modifiziert nach dem Code von Rings) ist bei mir mit Dateien auf der lokalen Festplatte meist etwas schneller als die PB-Variante.
Die Ergebnisse schwanken immer etwas, aber ich erhalte öfter sowas wie
Code: Alles auswählen
; PB 4.51
EnableExplicit
DisableDebugger
Procedure.i CountFiles_WinAPI (sPath.s)
Protected numFiles = 0
Protected handle
Protected f.WIN32_FIND_DATA
If Right(sPath, 1) <> "\"
sPath + "\"
EndIf
sPath + "*.*"
; start a file enum in the specified path
handle = FindFirstFile_(sPath, f)
If handle = #INVALID_HANDLE_VALUE
ProcedureReturn 0
EndIf
; exclude subdirectory from count
If (f\dwFileAttributes & #FILE_ATTRIBUTE_DIRECTORY) = 0
numFiles = 1
Else
; it is a directory, recursion here if needed
EndIf
While FindNextFile_(handle, f)
If (f\dwFileAttributes & #FILE_ATTRIBUTE_DIRECTORY) = 0
numFiles + 1
Else
; it is a directory, recursion here if needed
EndIf
Wend
; close the file search
FindClose_(handle)
ProcedureReturn numFiles
EndProcedure
Procedure.i CountFiles_PB (sPath.s)
Protected numFiles = 0
Protected handle
If Right(sPath, 1) <> "\"
sPath + "\"
EndIf
; start a file enum in the specified path
handle = ExamineDirectory(#PB_Any, sPath, "*.*")
If handle = 0
ProcedureReturn 0
EndIf
While NextDirectoryEntry(handle)
If DirectoryEntryType(handle) = #PB_DirectoryEntry_File
numFiles + 1
Else
; it is a directory, recursion here if needed
EndIf
Wend
; close the file search
FinishDirectory(handle)
ProcedureReturn numFiles
EndProcedure
; -- Test
Define path$, count1, count2
Define.q time0, time1, freq, t1, t2
path$ = "C:\windows\system32\"
QueryPerformanceCounter_(@time0) ; use high-resolution performance counter
count1 = CountFiles_WinAPI(path$)
QueryPerformanceCounter_(@time1)
QueryPerformanceFrequency_(@freq)
t1 = (time1 - time0) * 1000000 / freq
QueryPerformanceCounter_(@time0) ; use high-resolution performance counter
count2 = CountFiles_WinAPI(path$)
QueryPerformanceCounter_(@time1)
QueryPerformanceFrequency_(@freq)
t2 = (time1 - time0) * 1000000 / freq
EnableDebugger
Debug "WinAPI:"
Debug Str(count1) + " Dateien"
Debug Str(t1) + " Mikrosekunden"
Debug ""
Debug "PB:"
Debug Str(count2) + " Dateien"
Debug Str(t2) + " Mikrosekunden"
Grüße, NinoWinAPI:
2347 Dateien
2834 Mikrosekunden
PB:
2347 Dateien
2455 Mikrosekunden