Ant*_*neB 5 c++ optimization function-pointers switch-statement
在我的学校,我们强烈建议在C++(和C)中使用指向成员的指针数组而不是switch(或多个if).
因为我没有看到使用这样的数组的任何意义(我实际上使用指向成员的指针的映射)而不是switch语句,我想知道是否有任何类型的优化会推荐指向函数的指针.
这就是让我觉得使用开关更好的原因:
成员指针的数组(特别是映射)是内存繁重的(std :: string作为键,指针作为值),需要存储在类中(没有任何意义,因为它不是对象属性.. .)或者在函数中每次使用它们重新创建(如果静态声明):
std::map<std::string, void (MyClass::*)(...)> operations;
Run Code Online (Sandbox Code Playgroud)他们很难实例化并准备好使用:
operations.insert(std::map<std::string, void (Parser::*)(std::vector<std::string> const &)>::value_type("push", &Parser::push));
operations.insert(std::map<std::string, void (Parser::*)(std::vector<std::string> const &)>::value_type("pop", &Parser::pop));
operations.insert(std::map<std::string, void (Parser::*)(std::vector<std::string> const &)>::value_type("dump", &Parser::dump));
operations.insert(std::map<std::string, void (Parser::*)(std::vector<std::string> const &)>::value_type("assert", &Parser::assert));
operations.insert(std::map<std::string, void (Parser::*)(std::vector<std::string> const &)>::value_type("add", &Parser::add));
operations.insert(std::map<std::string, void (Parser::*)(std::vector<std::string> const &)>::value_type("sub", &Parser::sub));
operations.insert(std::map<std::string, void (Parser::*)(std::vector<std::string> const &)>::value_type("mul", &Parser::mul));
operations.insert(std::map<std::string, void (Parser::*)(std::vector<std::string> const &)>::value_type("div", &Parser::div));
operations.insert(std::map<std::string, void (Parser::*)(std::vector<std::string> const &)>::value_type("mod", &Parser::pop));
operations.insert(std::map<std::string, void (Parser::*)(std::vector<std::string> const &)>::value_type("print", &Parser::print));
operations.insert(std::map<std::string, void (Parser::*)(std::vector<std::string> const &)>::value_type("exit", &Parser::exit));
Run Code Online (Sandbox Code Playgroud)它会强制你在某些函数中使用无用的参数,并使非const成员可以成为const.例如,在我之前的代码中,"print"和"assert"可能是const,如果它们没有在地图中使用,并且大多数函数不使用参数,而是"push"和"assert"是...
您必须验证您要使用的指针是否存在于地图中,而不是仅让"默认"案例处理它,并且调用很难阅读:
if (operations.find(myOperation) != operations.end())
(this->*(operations.find(myOperation)->second))(myParameter);
Run Code Online (Sandbox Code Playgroud)那么为什么我们被迫使用指向成员的指针而不仅仅是一个明确的switch语句,甚至是else-ifs?
谢谢.
这取决于。具有多个未连接选项的Switch-case实际上与 big if-else-slow相同。好的优化是使用偏移表(或跳转表)执行所需的操作,建议您实现。
奇怪的是,编译器通常可以自动执行这种优化——如果switch-case写得好的话。
但是写得好意味着什么?
这意味着,您必须设计条目索引,因此可以轻松快速地计算需要执行的条目的位置。考虑以下代码:
int n = 0;
std::cin >> n;
if(n == 1) printf("1\n");
else if(n == 2) printf("2\n");
else if(n == 3) printf("3\n");
else if(n == 4) printf("4\n");
Run Code Online (Sandbox Code Playgroud)
这是可能的输出(实际输出,在 VC11 上,使用 /O2 编译):
011AA799 mov eax,dword ptr [n]
011AA79C cmp eax,1 //is n equal to 1?
011AA79F jne main+34h (011AA7B4h) //if yes, continue, if not, jump... [J1]
011AA7A1 push 1262658h
011AA7A6 call printf (011E1540h) // print 1
011AA7AB add esp,4
011AA7AE xor eax,eax
011AA7B0 mov esp,ebp
011AA7B2 pop ebp
011AA7B3 ret
011AA7B4 cmp eax,2 // [J1] ...here. Is n equal to 2?
011AA7B7 jne main+4Ch (011AA7CCh) //If yes, continue, if not, jump... [J2]
011AA7B9 push 126265Ch
011AA7BE call printf (011E1540h) // print 2
011AA7C3 add esp,4
011AA7C6 xor eax,eax
011AA7C8 mov esp,ebp
011AA7CA pop ebp
011AA7CB ret
011AA7CC cmp eax,3 // [J2] ...here. Is n equal to 3? (and so on...)
011AA7CF jne main+64h (011AA7E4h)
011AA7D1 push 1262660h
011AA7D6 call printf (011E1540h)
[...]
Run Code Online (Sandbox Code Playgroud)
基本上 - 一个if-else。现在,让我们更改我们的代码:
int n = 0;
std::cin >> n;
switch(n)
{
case 1: printf("1\n"); break;
case 2: printf("2\n"); break;
case 3: printf("3\n"); break;
case 4: printf("4\n"); break;
}
Run Code Online (Sandbox Code Playgroud)
可能的输出:
011BA799 mov eax,dword ptr [n] // switch case will run if n is 1-4
011BA79C dec eax //decrement by one, now it should be in 0-3
011BA79D cmp eax,3 // compare with 3
011BA7A0 ja $LN4+46h (011BA7EFh) //if greater than 3, skip switch
011BA7A2 jmp dword ptr [eax*4+11BA7F8h] //otherwise compute offset of instrcution and jump there
Run Code Online (Sandbox Code Playgroud)
我没有发布呼叫printf- 基本上相同,但没有任何cmp或跳转指令。
这个输出当然只是众多可能中的一种,但重点是:精心设计的应用程序在条件部分上进行了智能优化,可以更高效地执行。在这里,编译器能够直接跳转到正确的指令,因为可以轻松计算其偏移量 - 所有情况都用数字标记,数字增加一。
更直接地回答你的问题:你得到的建议在技术上是正确的,但不是复杂的代码(可能会或可能不会显着提高速度),我会专注于编译器友好的优化,而不是每个人都能理解和依赖(只要编译器足够聪明,可以利用这个优势并生成优化的代码)。