用#ifdef或类似的东西替换开关/外壳

Asm*_*aAP -1 c conditional-compilation switch-statement

我试图switch/case通过其他工具替换结构做同样的事情,但具有更好的性能(更少的执行时间...),我想到的#ifdef方法,但我不知道如何在这种情况下使用它:

float k_function(float *x,float *y,struct svm_model model)
{
    int i;
    float sum=0.0;
    switch(model.model_kernel_type)  
    {
    case LINEAR :
        return result1;
    case POLY :
        return result2;
    case RBF :
        return result3;
    case SIGMOID :
        return result4;
    default :
        return 0;
    }
}
Run Code Online (Sandbox Code Playgroud)

PS:

typedef   enum   kernel_type   {LINEAR, POLY, RBF, SIGMOID};
Run Code Online (Sandbox Code Playgroud)

rit*_*lew 8

正如我已经评论过的,我不相信预处理器语句是您正在寻找的.要使用预处理器条件,model.model_kernel_type需要使用#define语句定义常量.

我不知道switch语句的内部结构,因为它可能是O(n)或O(1),具体取决于编译器如何处理它.如果你需要确定O(1)时间复杂度,你可以简单地用你的查找表替换你的switch语句:

float model_type_results[4] = {result1, result2, result3, result4};

...

return model_type_results[model.model_kernel_type];
Run Code Online (Sandbox Code Playgroud)