哪种虚拟机的调度方法更有效?

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版.

其中哪一个转换为开销最小的更高效的汇编程序,或者它们是否相同?

先感谢您!

Adr*_*thy 5

知道哪个更快的唯一方法是测量.

优化器可能能够用这两种技术做很多事情.密集开关语句通常简化为跳转表,函数调用可以内联,因此这可能是最快的方法.

但是,如果由于某种原因,优化器无法内联或者如果switch语句变成if-else语句的级联,那么函数指针调用可能会更快.

维基百科有一篇关于线程代码的文章,它描述了在虚拟机或解释器中分派操作码的各种技术.