Time complexity for an OS to find a file/directory?

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
11 replies · 4K views
kolleamm
Messages
476
Reaction score
44
What is the time complexity for an OS to find a file? Is it O(1) time?

Let's say for example, you had a billion files in a single folder, and you wanted to load a file into a string in your program, would the system find a specific file right away, or would there be a longer wait?

Let's assume an OS like Windows.
 
Physics news on Phys.org
kolleamm said:
What is the time complexity for an OS to find a file? Is it O(1) time?

Let's say for example, you had a billion files in a single folder, and you wanted to load a file into a string in your program, would the system find a specific file right away, or would there be a longer wait?

Let's assume an OS like Windows.
Older OS (since Windows 2000 or NT 4) have the utility for files to be indexed, therefore time to find a file in indexed database is ~ln(N), which is slightly slower than O(0). The indexing is active by default and run fully automatically in Windows 7 or Windows 10, but is absent in Windows 8.
Most of Unix/Linux versions also use a sort of indexing/hashing of file tree.
 
  • Like
Likes   Reactions: kolleamm
trurle said:
Older OS (since Windows 2000 or NT 4) have the utility for files to be indexed, therefore time to find a file in indexed database is ~ln(N), which is slightly slower than O(0). The indexing is active by default and run fully automatically since Windows 7.
Do you mean O (log n)? Which would be the time complexity for a binary tree search.
 
  • Like
Likes   Reactions: trurle
kolleamm said:
Do you mean O (log n)? Which would be the time complexity for a binary tree search.
Yes, exactly
 
trurle said:
Yes, exactly
Ah I see, and by indexing do you mean alpha numerical order? I'm guessing by the ascii values of the characters?
 
kolleamm said:
Ah I see, and by indexing do you mean alpha numerical order? I'm guessing by the ascii values of the characters?
As i know, indexing is done by hashing the filename, and then by searching binary tree of hashes. Because hashes are shorter than full-path filenames, (4 to 16 bytes), the search is faster. Of course, this way do not work for templates, only for exact file names.
 
  • Like
Likes   Reactions: kolleamm
trurle said:
As i know, indexing is done by hashing the filename, and then by searching binary tree of hashes. Because hashes are shorter than filenames, (4 to 16 bytes), the search is faster. Of course, this way do not work for templates, only for exact file names.
Wow I never knew, thanks for the info!
 
anorlunda said:
Donald Knuth's "The Art and Science of Computer Programming, Volume 3"

"THE ART OF COMPUTER PROGRAMMING, Volume 3/Sorting and Searching" Donald Knuth, ADDISON-WESLEY PUBLISHING COMPANY, Reading, Massachusetts. 1973, ISBN 0-201-03803-X

There are later editions.

'The Bible.' Highly recommended!

Cheers,
Tom
 
  • Like
Likes   Reactions: Klystron, FactChecker and anorlunda
It's a huge text tree search. Lots of possible searches, and even if indexing, lots of ways to index (and keep an index updated). Worst case can be quite bad, especially if a file was just added and hasn't made it into the index yet, or if you are using regular expressions and there are a gazillion candidates for consideration.
 
But wouldn't a hash table lookup be O(1)?
 
Well, yeah, in the 'ideal' implementations. Once you hit the 'real' world there are either collisions or you are over provisioning the algorithm, the storage space, or both.
 
  • Like
Likes   Reactions: Vanadium 50 and DanielChin