3

I have a function to get a FileSize of a file. I am running this on WinCE. Here is my current code which seems particularily slow

int Directory::GetFileSize(const std::string &filepath)
{
    int filesize = -1;

#ifdef linux
    struct stat fileStats;
    if(stat(filepath.c_str(), &fileStats) != -1)
      filesize = fileStats.st_size;
#else
    std::wstring widePath;
    Unicode::AnsiToUnicode(widePath, filepath);

    HANDLE hFile = CreateFile(widePath.c_str(), 0, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
    if (hFile > 0)
    {
      filesize = ::GetFileSize( hFile, NULL); 
    }

    CloseHandle(hFile);
#endif

    return filesize;
}
3
  • 2
    You may want to confirm that this is the actual bit of code that is slow. I don't see any particular reason this would be slow unless your filesystem contains a huge amount of files [and thus, searching through all the files takes a while before it can be opened]. Jan 31, 2013 at 14:53
  • @MatsPetersson You have a point, as I posted this I realised it may be the UnicodeToAnsi function as it involves string operations. I refactored code to use less of these and have improved the speed by 30% already
    – Chris
    Jan 31, 2013 at 14:56
  • If you are doing "many" AnsiToUnicode calls, you may want to build a cache that checks if you have already done this file and returns the previous one... Some hashing may help here. Jan 31, 2013 at 14:59

4 Answers 4

11

At least for Windows, I think I'd use something like this:

__int64 Directory::GetFileSize(std::wstring const &path) { 

    WIN32_FIND_DATAW data;
    HANDLE h = FindFirstFileW(path.c_str(), &data);
    if (h == INVALID_HANDLE_VALUE)
        return -1;

    FindClose(h);

    return data.nFileSizeLow | (__int64)data.nFileSizeHigh << 32;
}

If the compiler you're using supports it, you might want to use long long instead of __int64. You probably do not want to use int though, as that will only work correctly for files up to 2 gigabytes, and files larger than that are now pretty common (though perhaps not so common on a WinCE device).

I'd expect this to be faster than most other methods though. It doesn't require opening the file itself at all, just finding the file's directory entry (or, in the case of something like NTFS, its master file table entry).

4
  • From the file system driver's viewpoint, a directory is also a file. Opening a directory must not be faster than opening a regular file.
    – xmllmx
    Jan 31, 2013 at 15:41
  • This works a treat, thankyou! With a combination of this and reducing calls to AnsiToUnicode I have halved the time it took to index before. Each change contributing equally.
    – Chris
    Jan 31, 2013 at 15:41
  • @Jerry, you are correct. Your solution indeed saves a time of opening.
    – xmllmx
    Jan 31, 2013 at 15:46
  • It also means you dont have to seek in the file either. For me also, as I am using the code to calculate file sizes of many files I already have access to WIN32_FIND_DATAW already so that operation is "free" too :D
    – Chris
    Jan 31, 2013 at 15:56
1

Your solution is already rather fast to query the size of a file.

Under Windows, at least for NTFS and FAT, the file system driver will keep the file size in the cache, so it is rather fast to query it. The most time-consuming work involved is switching from user-mode to kernel-mode, rather than the file system driver's processing.

If you want to make it even faster, you have to use your own cache policy in user-mode, e.g. a special hash table, to avoid switching from user-mode to kernel-mode. But I don't recommend you to do that, because you will gain little performance.

PS: You'd better avoid the statement Unicode::AnsiToUnicode(widePath, filepath); in your function body. This function is rather time-consuming.

1

Just an idea (I haven't tested it), but I would expect GetFileAttributesEx to be fastest at the system level. It avoids having to open the file, and logically, I would expect it to be faster than FindFirstFile, since it doesn't have to maintain any information for continuing the search.

-1

You could roll your own but I don't see why your approach is slow:

int Get_Size( string path )
{
// #include <fstream>
FILE *pFile = NULL;

// get the file stream
fopen_s( &pFile, path.c_str(), "rb" );

// set the file pointer to end of file
fseek( pFile, 0, SEEK_END );

// get the file size
int Size = ftell( pFile );

// return the file pointer to begin of file if you want to read it
// rewind( pFile );

// close stream and release buffer
fclose( pFile );

return Size;
}
1
  • 3
    I very much doubt that adding several layers of C library code would make it any faster. Jan 31, 2013 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.