PDA

View Full Version : exception catching speed



stefan
24th October 2008, 09:25
hi,
since i implemented exception catching in my code, it executes about 50% longer than before. Since it's numerical calculation that runs for couple of hours, speed is quite important. number of try-catch blocks is less than 10.

how much exception catching affects on speed?

Kumosan
24th October 2008, 10:23
hi,
since i implemented exception catching in my code, it executes about 50% longer than before. Since it's numerical calculation that runs for couple of hours, speed is quite important. number of try-catch blocks is less than 10.

how much exception catching affects on speed?

What are you doing? A quick google search revealed that an unused try/catch block has a performance impact, but only a very little one. If your code regularly throws an exception, consider a redesign. Exceptions are not to be used to control the normal program flow.

stefan
24th October 2008, 10:43
What are you doing? A quick google search revealed that an unused try/catch block has a performance impact, but only a very little one. If your code regularly throws an exception, consider a redesign. Exceptions are not to be used to control the normal program flow.

im using external solver (program) with whom i comunicate trough ascii files. execution of solver is called about 5 times in a second for couple of hours. since i'm working with files, it's good to catch exceptions, isn't it?

whats about dividing one try-catch to more of them?

stefan
24th October 2008, 10:46
i forgot to mention that exception are rarely thrown. maybe couple of times during one day analysis. since we implemented catching, a one hour analysis now lasts around one houre and forty minutes.

Kumosan
24th October 2008, 10:54
im using external solver (program) with whom i comunicate trough ascii files. execution of solver is called about 5 times in a second for couple of hours. since i'm working with files, it's good to catch exceptions, isn't it?

5 times/sec? You should not even be able to notice the impact of an unused try/catch block in that case.



whats about dividing one try-catch to more of them?

I really doubt your problem is because of an unused try/catch block. But if it is, having more would only increase your problem.

I'd say now is a good time to fire up your profiler. :)

stefan
24th October 2008, 11:09
5 times/sec? You should not even be able to notice the impact of an unused try/catch block in that case.

i don't notice slowdown on one or even on 10 runs o external program (its less than a second)

but when i run a hounderd thousands of runs (= about 5 hours without catching) it is running for more then seven hours!

Kumosan
24th October 2008, 11:59
I have not found a pure c++ example. Mostly C# or like in this example gcj:
http://gcc.gnu.org/ml/java/2004-08/msg00036.html

You see here with 10,000,000 calls in a for loop it makes a noticeable difference. But still in the range of seconds. I really doubt (but do not actually know) that it is different for pure C++ regardless of what compiler you use.

Your problem must be somewhere else. After work I'll write a small test program myself. The question somewhat interests me.

stefan
24th October 2008, 13:56
thank you for your time.
i doubt that i done some change to the code that is influencing on speed so much. all i did during last days is exception catching..

stefan
24th October 2008, 14:52
i found a accidentally pause in my code :( i put it there to test catching of exceptions, so we can blame catching of exceptions for slowdown ;)

Kumosan
27th October 2008, 21:11
i found a accidentally pause in my code :( i put it there to test catching of exceptions, so we can blame catching of exceptions for slowdown ;)

Great. Nevertheless, I did some experiments and my 'findings' are a bit surprising.


unsigned int diff = 1000000;
QTime t;
t.start();
for(unsigned int i = 0; i < diff; ++i){
usleep(1);
}
qDebug("Time elapsed: %d ms", t.elapsed());


Execution time: 3894ms



unsigned int diff = 1000000;
QTime t;
t.start();
try{
for(unsigned int i = 0; i < diff; ++i){
usleep(1);
}
}catch(int bla){
(void) bla;
}
qDebug("Time elapsed: %d ms", t.elapsed());


Execution time: 7708ms



unsigned int diff = 1000000;
QTime t;
t.start();
try{
for(unsigned int i = 0; i < diff; ++i){
usleep(1);
}
}catch(int bla){
(void) bla;
}catch(int bla2){
(void) bla2;
}

qDebug("Time elapsed: %d ms", t.elapsed());


Execution time: 7743ms

So the try/catch block has a noticeable influence on speed, but
it looks like the effect of a second catch is in most cases negligible.

stefan
12th November 2008, 19:58
Great. Nevertheless, I did some experiments and my 'findings' are a bit surprising.


unsigned int diff = 1000000;
QTime t;
t.start();
for(unsigned int i = 0; i < diff; ++i){
usleep(1);
}
qDebug("Time elapsed: %d ms", t.elapsed());


Execution time: 3894ms



unsigned int diff = 1000000;
QTime t;
t.start();
try{
for(unsigned int i = 0; i < diff; ++i){
usleep(1);
}
}catch(int bla){
(void) bla;
}
qDebug("Time elapsed: %d ms", t.elapsed());


Execution time: 7708ms



unsigned int diff = 1000000;
QTime t;
t.start();
try{
for(unsigned int i = 0; i < diff; ++i){
usleep(1);
}
}catch(int bla){
(void) bla;
}catch(int bla2){
(void) bla2;
}

qDebug("Time elapsed: %d ms", t.elapsed());


Execution time: 7743ms

So the try/catch block has a noticeable influence on speed, but
it looks like the effect of a second catch is in most cases negligible.

thanks, very simple benchmark, but results are very interesting.
i suppose nested try blocks are even more slower..