Could it be that your memory (the @files Array) is exhausted?
Or do you "some stuff" with the System (which is it?) where
you allocate and never free Resources? Or is it an CGI which
needs longer for 10000 files then for 20 and TIMEOUT occurs?
Did you account for 20000 files need 500000 times the time which
20 files need for opening?