Re: http://msdn.microsoft.com/en-us/library/system.io.file.exists.aspx
Does anyone know if this is a particularly slow or locking operation which could impact server performance in a large environment?
Re: http://msdn.microsoft.com/en-us/library/system.io.file.exists.aspx
Does anyone know if this is a particularly slow or locking operation which could impact server performance in a large environment?
Locking no. Slow, depends on what you re comparing it to. It s fairly cheap as far as I/O goes, but I/O is generally slow overall compared to other operations. So, if you must use it, it won t hurt too bad. However, I d try not to call it more times than is truly necessary! :-)
In computing, there is actually no such thing as an "expensive operation", unless you consider what it is expensive in relation to.
For instance, in the real world, would $2.000.000 for an object be expensive? What if it is the price of Bahamas? Would it be expensive then? What about for a carton of milk? Is that expensive?
The thing you need to consider is if File.Exists
is expensive in terms of the overall operation you intend to do, and whether or not you actually have any alternatives.
If you don t have any alternatives, does it matter if it is expensive or not?
For instance, if you do 1 check if the file exists, and then if it does, you load it in, and spend an hour processing it, then I would assume it would not be considered expensive.
However, if you call it 10 times in a single loop, to figure out if a file exists, and then if it does, just increment a number, then it might be the most expensive single operation you do there.
The only way you can know for sure is to actually measure how long that method call takes, compared to what else you in the same operation.
In year 2016 it doesn t seem to be very expensive and there also seem to be no real difference between File.Exists
and PathFileExists
(Why is File.Exists() much slower when the file does not exist?). The only difference I could measure is that it s faster to check for a non-existing file then an existing one:
(Tested on an SSD)
[DllImport("Shlwapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
private extern static bool PathFileExists(StringBuilder path);
void Main()
{
var sw = Stopwatch.StartNew();
for (int i = 0; i < 10000; i++)
{
File.Exists(@"c:HomeTemp est_.log");
}
sw.Stop();
sw.Dump("File.Exists = false");
sw = Stopwatch.StartNew();
for (int i = 0; i < 10000; i++)
{
File.Exists(@"c:HomeTemp est.log");
}
sw.Stop();
sw.Dump("File.Exists = true");
var sb = new StringBuilder(@"c:HomeTemp est_.log");
sw = Stopwatch.StartNew();
for (int i = 0; i < 10000; i++)
{
PathFileExists(sb);
}
sw.Stop();
sw.Dump("PathFileExists = false");
sb = new StringBuilder(@"c:HomeTemp est.log");
sw = Stopwatch.StartNew();
for (int i = 0; i < 10000; i++)
{
PathFileExists(sb);
}
sw.Stop();
sw.Dump("PathFileExists = true");
}
I don t think it is (file operations are heavily optimized and cached on most OS s) and most of the other operations are more likely to be culprits here (sockets, DB access, general processing, etc). But, as usual, the best way is to actually profile your application and see if it s an hotspot.
File.Exisits
with kernel32.dll FindFirstFile open handler to the file. If resulting handle is invalid, it return false. If valid it fill data structure with all stuff like LastAccessTime, CreationTime, file size and so on. And then return true. Nothing blocking.
Best would be to run some tests in your environment. I have an app that can do 10,000+ per second without a hiccup to my systems. I consider that pretty fast.
As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...
I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...
I have two EF entities. One has a property called HouseNumber. The other has two properties, one called StartHouseNumber and one called EndHouseNumber. I want to create a many to many association ...
I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....
I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...
I ve got some code which sets up a datacontext. Often enough, the datacontext should be set to some underlying data collection, such as an ObservableCollection - but occasionally I d like to set it ...
I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...
NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...