Language-independent – Is it really performance loss when capturing abnormalities?

I asked a question about exceptions, and I am very annoyed by those who say that throwing is slow. I have asked in the past in How exceptions work behind the scenes, and I know that in normal code There are no additional instructions in the path (as the accepted answer says) but I am not entirely convinced that throwing is more expensive than checking the return value. Consider the following:

{< br /> int ret = func();
if (ret == 1)
return;
if (ret == 2)
return;
doSomething( );
}

VS

{
try{
func();
doSomething( );
}
catch (SpecificException1 e)
{
}
catch (SpecificException2 e)
{
}
}

As far as I know, there is no difference except that ifs is moved out of the normal code path into the exception path and an extra jump or two paths to the exception code. When it reduces the main and frequently running code For a few ifs in the path, the extra jumps or two don’t sound much. Are those anomalies actually slow? Or is this a myth of old compilers or an old problem?

(I’m talking about general exceptions. Specifically, exceptions in compiled languages, such as C and D; although C# is also in my mind.)

< div class="content-split">

Okay-I just ran some tests to make sure that the exception is actually slower. Introduction: On my machine, every iteration Calling w/return is 30 cycles. Each iteration throws w/catch for 20370 cycles.

So to answer this question-yes-throwing exceptions is very slow.

This is the test code:

#include 
#include

int Test1 ()
{
throw 1;
// return 1;
}


int main(int argc, char*argv[ ])
{
int result = 0;
__int64 time = 0xFFFFFFFF;
for(int i=0; i<10000; i++)
{
__int64 start = __rdtsc();
try
{
result += Test1();
}
catch(int x)
{
result += x;
}
__int64 end = __rdtsc();
if(time> end-start)
time = end-start;
}

printf("%d ", result);
printf("time: %I64d ", time);
}

Another try/capture by o p write

try
{
if(Test1()!=0)
result++;
}
catch(int x)
{
result++;

I asked a question about exceptions, and I am very much to those who say that throwing is slow Annoyed. I have asked in the past in How exceptions work behind the scenes, and I know that there are no additional instructions in the normal code path (as the accepted answer says) but I am not entirely convinced that throwing is more expensive than checking the return value. Consider the following :

{
int ret = func();
if (ret == 1)
return;
if (ret == 2)
return;
doSomething();
}

VS

{ 
try{
func();
doSomething();
}
catch (SpecificException1 e)
{
}
catch (SpecificException2 e)
{
}
}

As far as I know, except ifs are moved out of the normal code path into the exception path and an extra jump or There is no difference between the two outside the code path of the exception. When it reduces a few ifs in the main and frequently run code path, the extra jumps or two do not sound much. Are those exceptions actually slow? Or is this a myth of old compilers or an old problem?

(I’m talking about general exceptions. Specifically, exceptions in compiled languages, such as C and D; although C# is also in my mind.)

< p>

Ok-I just ran some tests to make sure that the exception is actually slower. Introduction: On my machine, each iteration call w/return is 30 loops. Each iteration Throwing w/catch is 20370 cycles.

So to answer this question-yes-throwing exceptions is very slow.

This is the test code:

#include 
#include

int Test1()
{
throw 1;
// return 1;
}


int main(int argc, char*argv[])
{
int result = 0;
__int64 time = 0xFFFFFFFF;
for(int i=0; i<10000; i++)
{
__int64 start = __rdtsc();
try
{
result += Test1();
}
catch(int x)
{
result += x;
}
__int64 end = __rdtsc();
if(time> end-start)
time = end-start;
}

printf("% d ", result);
printf("time: %I64d ", time);
}

Another attempt/capture is written by op

try
{
if(Test1()!= 0)
result++;
}
catch(int x)
{
result++;

Leave a Comment

Your email address will not be published.