3

我用 C 编写了一个简单的 VM,使用简单的指令切换,没有任何指令解码,但性能很糟糕。

对于简单的算术运算,对于相同的操作,VM 比本地 C 代码慢大约 4000 倍。我测试了一组长度为 1000 万的数组,第一个由程序指令组成,随机 + - * / 操作,2 个保存随机整数的数组,第三个数组是操作目标存储。

我原以为算术性能会下降 3-4 倍,所以 `4000x 真的让我大吃一惊。即使是最慢的解释语言似乎也能提供更高的性能。那么我的方法哪里出了问题,如何在不将 JIT 编译为机器代码的情况下提高性能?

实现是......基本上是我能想到的最简单的:

begin:
    {
        switch (*(op+(c++)))
        {
        case 0:
            add(in1+c, in2+c, out+c); goto begin;

        case 1:
            sub(in1+c, in2+c, out+c); goto begin;

        case 2:
            mul(in1+c, in2+c, out+c); goto begin;

        case 3:
            div(in1+c, in2+c, out+c); goto begin;

        case 4:
            cout << "end of program" << endl;
            goto end;

        default:
            cout << "ERROR!!!" << endl;

        }
    }

end:

更新:当我注意到我用来分析的 QElapsedTimer 实际上被破坏时,我正在玩弄程序的长度。现在我正在使用 clock() 函数,根据它计算出的 goto 实际上与本机代码运行相当,可能会低一点。这个结果合法吗???这是完整的源代码(我知道它很丑,毕竟它只是为了测试):

#include <QtGlobal>
#include <iostream>
#include <stdio.h>
#include <ctime>

using namespace std;

#define LENGTH 70000000

void add(int & a, int & b, int & r) {r = a * b;}
void sub(int & a, int & b, int & r) {r = a - b;}
void mul(int & a, int & b, int & r) {r = a * b;}
void div(int & a, int & b, int & r) {r = a / b;}

int main()
{
    char * op = new char[LENGTH];
    int * in1 = new int[LENGTH];
    int * in2 = new int[LENGTH];
    int * out = new int[LENGTH];

    for (int i = 0; i < LENGTH; ++i)
    {
        *(op+i) = i % 4;
        *(in1+i) = qrand();
        *(in2+i) = qrand()+1;
    }

    *(op+LENGTH-1) = 4; // end of program


    long long  sClock, fClock;


    unsigned int c = 0;
    sClock = clock();

    cout << "Program begins" << endl;

    static void* table[] = {
        &&do_add,
        &&do_sub,
        &&do_mul,
        &&do_div,
        &&do_end,
        &&do_err,
        &&do_fin};

#define jump() goto *table[op[c++]]

    jump();
do_add:
    add(in1[c], in2[c], out[c]); jump();
do_sub:
    sub(in1[c], in2[c], out[c]); jump();
do_mul:
    mul(in1[c], in2[c], out[c]); jump();
do_div:
    div(in1[c], in2[c], out[c]); jump();
do_end:
    cout << "end of program" << endl; goto *table[6];
do_err:
    cout << "ERROR!!!" << endl; goto *table[6];
do_fin:

    fClock = clock();
    cout << fClock - sClock << endl;

    delete [] op;
    delete [] in1;
    delete [] in2;
    delete [] out;

    in1 = new int[LENGTH];
    in2 = new int[LENGTH];
    out = new int[LENGTH];

    for (int i = 0; i < LENGTH; ++i)
    {
        *(in1+i) = qrand();
        *(in2+i) = qrand()+1;
    }

    cout << "Native begins" << endl;

    sClock = clock();

    for (int i = 0; i < LENGTH; i += 4)
    {

        *(out+i) = *(in1+i) + *(in2+i);
        *(out+i+1) = *(in1+i+1) - *(in2+i+1);
        *(out+i+2) = *(in1+i+2) * *(in2+i+2);
        *(out+i+3) = *(in1+i+3) / *(in2+i+3);
    }

    fClock = clock();
    cout << fClock - sClock << endl;

    delete [] in1;
    delete [] in2;
    delete [] out;

    return 0;
}
4

1 回答 1

5

Darek Mihocka 有一篇关于在便携式 C 中创建快速解释器的优秀而深入的文章:http ://www.emulators.com/docs/nx25_nostradamus.htm

于 2012-07-30T12:41:16.747 回答