c - 如何在多个 strncmp 以下进行优化?

标签 c algorithm

我需要检查字符串以查看它是否与任何前缀匹配。 future 要比较的前缀数量会增加。所以我担心下面代码的性能。当需要检查大量字符串时,有哪些选项可以使其运行得更快?

int checkString(const char *name)
{
    if(!name) return 0;

    if(strncmp(name, "AE_", 3) == 0 ) return 1;                                                                              
    if(strncmp(name, "AEDZ_", 5) == 0 ) return 1;                                                                            
    if(strncmp(name, "EDPZ_", 5) == 0 ) return 1;                                                                            
    if(strncmp(name, "EFAN_", 5) == 0 ) return 1;                                                                            
    if(strncmp(name, "E_GCA", 5 ) == 0 ) return 1;                                                                           
    if(strncmp(name, "EFFAN_", 6) == 0 ) return 1;                                                                           
    if(strncmp(name, "EPDPZ_", 6) == 0 ) return 1;                                                                           
    if(strncmp(name, "EDDPZ_", 6) == 0 ) return 1;                                                                           
    if(strncmp(name, "ECADF_", 6) == 0 ) return 1;                                                                           
    if(strncmp(name, "EPCEA_", 6) == 0 ) return 1;                                                                           
    if(strncmp(name, "CFEXXX_", 7) == 0 ) return 1;                                                                          
    if(strncmp(name, "IFEXX_", 7) == 0 ) return 1;                                                                           
    if(strncmp(name, "EINFFAN_", 8) == 0 ) return 1;                                                                         
    if(strncmp(name, "NXXEFAN_", 8) == 0 ) return 1;                                                                         
    if(strncmp(name, "ENAEAZY_", 8) == 0 ) return 1;                                                                         
    if(strncmp(name, "EYYYYYY_", 8) == 0 ) return 1;                                                                         
    if(strncmp(name, "ENEOENUE_", 9) == 0 ) return 1;                                                                        
    /*
    more strncmp to be added.
    */

    return 0;
}       

最佳答案

一次性、提前设置:

regex_t re;
regcomp(&re, "^(AE_|AEDZ|_EDPZ_|EFAN_|E_GCA|" /*...*/ ")", REG_EXTENDED);

检查:

return regexec(&re, name, 0, 0, 0) == 0;

在任何良好的正则表达式实现中,regcomp 会将正则表达式编译为 DFA,该 DFA 在最长前缀的长度范围内执行多个步骤。

关于c - 如何在多个 strncmp 以下进行优化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52998114/

相关文章:

database - 可扩展哈希 : why does anyone use most significant bits?

java - 算法复杂度 : Is it the same to iterate an array from the start than from the end?

c - 使用结构体在函数之间传递数组信息

c - Swift 中的任意 C 回调包装器

c++ - 显示系列和输入位数,如果...输入 n=900,则输出将为 1 4 9 25 49 121 169 289 361 529 841 并且计数 =3

iphone - 如何检查iphone应用程序中的更大值(value)

c - 将指针分配给链表: does not contain data

c - 数组:越界计算

algorithm - 从随机数数组中的算术级数中查找连续数

algorithm - 如何获得可以使用给定数字组成的所有可能的 n 位数字?