Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

do exceptions reduce performance?

My application traverses a directory tree and in each directory it tries to open a file with a particular name (using File.OpenRead()). If this call throws FileNotFoundException then it knows that the file does not exist. Would I rather have a File.Exists() call before that to check if file exists? Would this be more efficient?

like image 246
akonsu Avatar asked Oct 15 '10 20:10

akonsu


People also ask

Does exception handling improve performance?

Using exceptions, for exceptional case handling, performs vastly better than what your test shows. Mecki is right. The question was about comparing relative speed between regular control flow (a return statement) and exceptions. If anything, exceptions should be thrown 100% of the time, and not only 50%.

Are exceptions inefficient?

"exceptions is always slow compared to other basic operations in the language, regardless of the programming language"... except in languages designed to compile use of exceptions into ordinary flow control.

Does try Except affect performance?

In general, wrapping your Java code with try/catch blocks doesn't have a significant performance impact on your applications. Only when exceptions actually occur is there a negative performance impact, which is due to the lookup the JVM must perform to locate the proper handler for the exception.

Do exceptions slow down code?

If not used correctly, exceptions can slow down your program, as it takes memory and CPU power to create, throw, and catch exceptions. If overused, they make the code difficult to read and frustrating for the programmers using the API.


1 Answers

Update

I ran these two methods in a loop and timed each:

void throwException()
{
    try
    {
        throw new NotImplementedException();
    }
    catch
    {
    }
}

void fileOpen()
{
    string filename = string.Format("does_not_exist_{0}.txt", random.Next());
    try
    {
        File.Open(filename, FileMode.Open);
    }
    catch
    {
    }
}

void fileExists()
{
    string filename = string.Format("does_not_exist_{0}.txt", random.Next());
    File.Exists(filename);
}

Random random = new Random();

These are the results without the debugger attached and running a release build :

Method          Iterations per second
throwException                  10100
fileOpen                         2200
fileExists                      11300

The cost of a throwing an exception is a lot higher than I was expecting, and calling FileOpen on a file that doesn't exist seems much slower than checking the existence of a file that doesn't exist.

In the case where the file will often not be present it appears to be faster to check if the file exists. I would imagine that in the opposite case - when the file is usually present you will find it is faster to catch the exception. If performance is critical to your application I suggest that you benchmark both apporaches on realistic data.

As mentioned in other answers, remember that even in you check for existence of the file before opening it you should be careful of the race condition if someone deletes the file after your existence check but just before you open it. You still need to handle the exception.

like image 102
Mark Byers Avatar answered Oct 05 '22 00:10

Mark Byers