- 函数指针和switch哪个更快?
switch语句将具有约30个case
,由从0到30枚举的无符号整数组成。
我可以这样做:
class myType
{
FunctionEnum func;
string argv[123];
int someOtherValue;
};
// In another file:
myType current;
// Iterate through a vector containing lots of myTypes
// ... for ( i=0; i < myVecSize; i ++ )
switch ( current.func )
{
case 1:
//...
break;
// ........
case 30:
// blah
break;
}
每次都使用func
通过开关。使用开关的好处在于,比使用30个函数代码更加有组织。
或者我也可以这样做(不太确定):
class myType
{
myReturnType (*func)(int all, int of, int my, int args );
string argv[123];
int someOtherValue;
};
我会有30个不同的函数,一开始将其中一个函数的指针赋值给myType。
- 哪个可能更快:switch语句还是函数指针?
每秒调用次数:约1000万次。我无法进行测试 - 那需要我重写整个程序。目前正在使用switch语句。
我正在构建一个解释器,希望比Python和Ruby更快 - 每个时钟周期都很重要!
myReturnType (*func)();
吗? - kennytm