为什么__try块降低了我的程序的速度
Why do __try blocks reduce speed of my program?
我一直在研究__try
/__except
结构对我来说是否足够快,并且遇到了一个奇怪的结果。
我惊讶地发现在__try
块中调用函数的速度是自己调用函数的两倍。
为什么会这样?
#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 for 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;
}
在询问问题时,您可能意味着您想使用__try
/__except
而不是try
/catch
。
__try
/__except
是否比try
/catch
更快或更慢并不重要,因为,__try
/__except
用于捕获SEH (windows生成的错误),而不是用于捕获一般异常。
对于你写的标准c++代码,你应该总是使用try
/catch
,而不是__try
/__except
。
try
/catch
是c++标准为处理一般c++异常所指定的。
相关文章:
- 当我在main中声明了我的2d数组时,为什么我的程序会退出
- 为什么我的程序在for循环中k=0时返回垃圾值
- 为什么它只打印双链接列表的第一个值,而我的程序却崩溃了
- 我的程序没有从文件中读取并输入数据
- 为什么我的C++程序的程序集输出充满了 .ascii,没有汇编代码?
- 我的程序有一个保存配置文件的GUI,如何双击此配置文件以直接加载带有配置数据的GUI?
- 为什么我的程序不能显示斐波那契级数?
- 为什么我的程序在使用预留后没有加速?
- 为什么如果我添加这一行,我的程序会不断询问值
- 为什么当我输入较大的数字时,我的程序会到达文件末尾?
- 这是使用回溯的 nqueen 问题,但我使用了动态 2d 数组,我的程序编译良好,但不返回任何输出
- 为什么我的程序在 O0 和 O2 的优化级别返回不同的结果
- 为什么我的 c++ 程序检查不是初始化的变量?
- 为什么我的程序在输入某个形状的面积的测量值后没有结束?
- 在 fork() 之后,我在我的程序中不断得到相同的 pid
- C++ - 为什么我的程序在再次调用函数后关闭?
- 我的程序将 26 个字母转换为 ascII 没有显示正确答案
- 为什么我的程序在读取/写入文件时会删除最重要的数字?
- 我需要如何更改我的程序以使用打开/关闭原则?
- 我的 c++ 程序似乎没有发现字符串和我拥有但输入使用 getline 的变量之间的比较