The following code takes over 100 seconds to run on a directory with a couple hundred…
- User
The following code takes over 100 seconds to run on a directory with a couple hundred thousand files. How could it be optimized? for (boost::filesystem::directory_entry &directoryEntry : boost::filesystem::directory_iterator(qualifiedPath)) { // look at all of the files in the directory if (boost::filesystem::is_regular_file(directoryEntry)) { auto fileSize = boost::filesystem::file_size(directoryEntry.path()); auto fileWriteTime = boost::filesystem::last_write_time(directoryEntry.path()); dataFileMap.emplace(fileWriteTime, directoryEntry.path()); if (directoryEntry.path().extension() == ".foo") { total_foo_files++; } else if (directoryEntry.path().extension() == ".bar") { total_upload_files++; } else if (directoryEntry.path().extension() == ".baz") { total_upload_files++; } dataUsage += fileSize; } }
Assistant
The main performance issue is that you're making redundant filesystem calls. Here are the key optimizations:
Primary Issues
directoryEntry.path()is called multiple times - Each call may involve string operations or path constructionboost::filesystem::file_size()makes a separate stat call - The directory iterator already has this information
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.