What is the fastest way to record the listed files in a directory in C?
OP poses 2 approaches

Iterate twice through opendir(), readdir(), closedir() with one malloc().

Iterate one through opendir(), readdir(), closedir() with multiple realloc().


For performance, I would use a 3rd option: linked list instead of using an array of struct dirent

Iterate once through opendir(), readdir(), closedir() with one malloc() per entry.


As with such performance issue: strive for reduced big O, not linear ones.