开发者

Why do __try blocks reduce speed of my program?

I have been researching whether the __try/__except construct is fast enough for my use, and encountered a strange result.

I was surprised to find that calling a function inside a __try block is twice as fast as calling it on its own.

Why is this?


 #include <iostream>


 using namespace std;

 //addiitonal statndart includes
 #include <windows.h>

 void function()
 {
    int a=0;
        for (int i=0;i<1000;i++)
            for(int j=0;j<1000000;j++)
            {
                if(0==i*j % 2)
                    a++;
                else
                    a--;
            }
cout << a<< endl;
 }
 //you can make 0 f开发者_JAVA百科or test wihtout try
 #define USE_TRY 1

 int main()
 {

DWORD time = 0;
time =timeGetTime();

#if USE_TRY
    __try{
        function();
    }
    __except(1)
    {
        cout <<"   exception handled"<< endl;
    }
#else
    function();
#endif

time =timeGetTime()-time;

cout<<"time = "<<time<<endl;

 }


While asking the question you probably mean you want to use __try/__except instead of try/catch.

Whether __try/__except works faster or slower than try/catch is unimportant because, __try / __except is for catching SEH (windows generated errors) not for catching general exceptions.

For the standard C++ code you write you should always use try/ catch and not __try / __except.

try/catch is what the C++ standard specifies for handling general C++ exceptions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜