tags:

views:

61

answers:

3

Which is more efficient if there are numerous checks?

bool exists=File.Exists(file);

or

bool exists= check db list of existing files;
+3  A: 

Of course the second one, since system calls like this one are expensive. It's better to use collective directory listing function and check for the file existence in the memory without file system attention.

doc
He didn't say it was in-memory. He said it's a db call. Since that's a remote call, it might not end up being all that much faster. I've suggested he try both, profile/measure the performance, and see which is quicker for his needs.
Neil Barnwell
Take it in terms of algorithm complexity. While checking each file is O(n), checking all the files at once is constant time operation or O(1). Then you need in-memory O(n) in both cases.
doc
+1  A: 

If you write the path of the file in a database it is way faster than requesting it by File.Exists, depending on number of files. if you request only one, its not a great diffrence. But if you check more files, a database request is faster and I think you will get the path to the file by database, right?

MaikL80
+2  A: 

It depends.

If you want to check existence of the file, you have no choice but to check the filesystem. Yes, this might be slower than the db call. Db calls are still remote inter-process calls though, and as such can be expensive in their own right.

The answer is to try both, profile, and optimise for the best answer for your specific scenario.

Neil Barnwell