cxong / tinydir

Lightweight, portable and easy to integrate C directory and file reader

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

tinydir_open_sorted is really slow under windows

jpjodoin opened this issue · comments

I have a folder of 8000 images in it. If I call tinydir_open_sorted on it's path, it will take as long as 2 minutes to retrieve the full list of file. I am under MSVC 2010 under windows 7 x64. Profiling shows 99.9% of the time spent in an unknown method. If I use tinydir_open instead and I sort the file myself using a vector and std::sort, the result will take much less than a second to return.

I think the problem is with the use of realloc, that must constantly allocate new block of memory compare to a std::vector that might handle the reallocation much better.

Here's the C++ version that run much faster (to compare with tinydir_open_sorted)
std::vectorstd::string dirList
tinydir_open(&dir, dirPath.c_str());
while (dir.has_next)
{
tinydir_file file;
tinydir_readfile(&dir, &file);
if(!file.is_dir)
dirList.push_back(std::string(file.name));
tinydir_next(&dir);
}

tinydir_close(&dir);
std::sort(dirList.begin(), dirList.end());
return dirList;

I think a good solution would be to count the number of file before and calling the allocation one time instead.

commented

Yes you are right the realloc is the most likely culprit. The current implementation is very inefficient as it reallocs every time the size grows. I think simply changing to a doubling of capacity will make it fast enough.

Yeah. That would most likely be a good enough improvement.

commented

I've decided to count the number of files beforehand; I tested on a folder with 10000 subfolders and this seemed the fastest:

Times in seconds
Previous: 78.579
Doubling realloc: 0.04
Counting: 0.019

Thanks for raising this issue!