Sol*_*tia 2 c++ performance assembly compiler-optimization vm-implementation
哪种更有效的调度方法可以使我的fetch-decode-execute次数更快一点?
为简单起见,我将其保持在最低限度,就像操作在1字节操作数上操作一样,例如只有两个操作数.
我现在使用的方法(简化)是:
typedef unsigned char byte;
vector<byte> _program = { INST::PUSH, 32, INST::POP};
enum INST {
PUSH =0, /*index=0*/
POP =1, /*index=1*/
}
//DISPATCHING METHOD #1
switch (curr_instruction) {
case INST::PUSH: {
/*declared inline*/ _push_to_stack(_program[instr_ptr+1]);
}
case INST::POP: {
/*declared inline*/ _pop_stack();
}
}
Run Code Online (Sandbox Code Playgroud)
或者使用函数指针表来执行'program'中的每条指令(vector/vector _program),如下所示:
typedef void (*voidptr)();
void hndl_push(){
/*declared inline*/ _push_to_stack(_program[instr_ptr+1]);
}
void hndl_push(){
/*declared inline*/ _pop_stack();
}
funcptr handlers[2] = {&hndl_push /*index=0*/, & hdnl_pop /*index=1*/}'
vector<byte> _program = { INST::PUSH, 32, INST::POP};
size_t instr_ptr=0;
//DISPATCHING METHOD #2
while (instr_ptr != _program.size()){
instr_ptr++;
_handlers[instr_ptr]();
}
Run Code Online (Sandbox Code Playgroud)
我使用的是VC++(Visual Studio)编译器,即2015版.
其中哪一个转换为开销最小的更高效的汇编程序,或者它们是否相同?
先感谢您!