enwik8 と enwik9 で正規表現を実行するコードを C で書いています。ベンチマーク目的で、他の言語でも同じアルゴリズムを作成しています。問題は、Python や他のコードでは 10 秒しかかからないのに 40 秒かかるため、C コードで何か間違ったことをしていることです。
私は何を忘れていますか?
#include <stdio.h>
#include <regex.h>
#define size 1024
int main(int argc, char **argv){
FILE *fp;
char line[size];
regex_t re;
int x;
const char *filename = "enwik8";
const char *strings[] = {"\bhome\b", "\bdear\b", "\bhouse\b", "\bdog\b", "\bcat\b", "\bblue\b", "\bred\b", "\bgreen\b", "\bbox\b", "\bwoman\b", "\bman\b", "\bwomen\b", "\bfull\b", "\bempty\b", "\bleft\b", "\bright\b", "\btop\b", "\bhelp\b", "\bneed\b", "\bwrite\b", "\bread\b", "\btalk\b", "\bgo\b", "\bstay\b", "\bupper\b", "\blower\b", "\bI\b", "\byou\b", "\bhe\b", "\bshe\b", "\bwe\b", "\bthey\b"};
for(x = 0; x < 33; x++){
if(regcomp(&re, strings[x], REG_EXTENDED) != 0){
printf("Failed to compile regex '%s'\n", strings[x]);
return -1;
}
fp = fopen(filename, "r");
if(fp == 0){
printf("Failed to open file %s\n", filename);
return -1;
}
while((fgets(line, size, fp)) != NULL){
regexec(&re, line, 0, NULL, 0);
}
}
return 0;
}