views:

295

answers:

1

Below is a rather simple function which counts how many files are on a machine. Called on "C:\", it takes about 5 seconds to run. Unless I haven't run it in a while or first run a ram-clearing program, in which case it takes 60 seconds or more. I wouldn't have thought it could be caching since I'm doing a new scan each time (i.e. starting a new intance of the program, since all it does is this scan), but perhaps it relates to memory allocation? Any ideas on how to make that fast run happen every time, or on why it can't be done? Other programs (e.g. SpaceMonger) manage to get a total count of files in 10s even when I clear my ram or wait a long time between runs. So, there is definitely a way to do this, though not necessarily in VB.

Private Function countFiles(Name As String) As Long
    On Error GoTo ErrorHandler
    DoEvents
    Const CurMthd = "countFiles"
          Dim retval As Long
13        Dim FindData As win.WIN32_FIND_DATA
14        Dim SearchPath As String
15        Dim FileName As String
17        Dim SearchHandle As Long
          If Right(Name, 1) <> "\" Then Name = Name & "\"
19        SearchPath = Name & "*.*"
20        SearchHandle = win.FindFirstFile(SearchPath, FindData)
          Do
              DoEvents
'             g_Cancel = True
              If g_Cancel Then
                countFiles = retval
                Exit Function
            End If
22            If SearchHandle = win.INVALID_HANDLE_VALUE Or SearchHandle =     ERROR_NO_MORE_FILES Then Exit Do
23            FileName = dsMain.RetainedStrFromPtrA(VarPtr(FindData.cFileName(0)))
24            If AscW(FileName) <> 46 Then
                    If (FindData.dwFileAttributes And win.FILE_ATTRIBUTE_DIRECTORY)     Then
                        retval = retval + countFiles(Name & FileName)
                    Else
                        retval = retval + 1
                    End If
28            End If
29        Loop Until win.FindNextFile(SearchHandle, FindData) = 0
    win.FindClose SearchHandle
    countFiles = retval
    Exit Function
ErrorHandler:
    Debug.Print "Oops: " & Erl & ":" & Err.Description
    Resume Next
End Function
+1  A: 

The operating system itself caches data read from disk. This is completely outside your program, and you don't really have any control over it. Thus, when you run your "ram clearing" program it clears out these caches. This is why those "ram clearing" programs are generally completely useless - as you can see, by emptying the cache it makes your program run slower.

davr
The "ram clearing" program was something I wrote myself as a quick and dirty test to try to narrow down the speed issue. It was very ugly and, because of the way it was written, tended to anger the system.
Brian
If the operating system caching were the reason why my program was slow, then why are other mechanisms used to count how many files are on the system so much faster? While you may be right about the issue, other software (and the OS itself) has solved this problem.
Brian