I have two simple code functions that compare two strings.
我有两个简单的代码函数来比较两个字符串。
The strategy is to compare a short string with a long string sequentially.
策略是按顺序比较短字符串和长字符串。
For example,
例如,
string1 = "ABCDABCDEFGAHAD",
string2 = "ABCD"
then compare string1
and string2
by sliding string2
until string2
's end is reached to string1
's end
然后通过滑动string2比较string1和string2,直到string2的末端到达string1的末端。
Here are the functions that I used First one is
这里是我使用的第一个函数。
void compare1(char* src , char* target , int src_size , int target_size , int* score){
int i , j;
for ( i = 0 ; i < src_size - target_size ; i++){
for(j=0; j < target_size ; j++){
if(src[i+j] == target[j]){
score[i]++;
}else{
continue;
}
}
}
}
and the second one is
第二个是。
void compare2(char* src , char* target , int src_size , int target_size , int* score){
int i , j;
char* dest = (char*)malloc(sizeof(char)*(target_size));
for( i = 0 ; i < src_size - target_size ; i++){
strncpy(dest,src,target_size);
for( j = 0 ; j < src_size ; j++){
if(dest[j] == target[j]){
score[i]++;
}else{
continue;
}
}
}
free(dest);
}
These two gave me different times, which are
这两个给了我不同的时间。
8393.00 ms from compare1
4415.00 ms from compare2
what makes this time difference?
是什么造成了时差?
the length of two strings are 1024*1024*10 and 128 with respect to src and target in function variable.
两个字符串的长度是1024*1024*10和128关于src和函数变量的目标。
1 个解决方案
#1
3
Your second function is wrong. It should copy from src + i
not from src
. Also the nested loop should be done for the target_size
elements of dest
, not for src_size
as in your code. This results in:
你的第二个功能是错误的。它应该从src + i复制而不是src。还应该为target_size元素执行嵌套循环,而不是在代码中使用src_size。这将导致:
void compare2(char* src , char* target , int src_size , int target_size , int* score){
int i , j;
char* dest = (char*)malloc(sizeof(char)*(target_size));
for( i = 0 ; i < src_size - target_size ; i++){
strncpy(dest,src + i,target_size);
for( j = 0 ; j < target_size ; j++){
if(dest[j] == target[j]){
score[i]++;
}else{
continue;
}
}
}
free(dest);
}
Remark 1: the else continue;
is useless ! Remark 2: you can use memcpy
to copy the string (since you know the length and don't use the terminal character '\0'
).
注1:else继续;是无用的!备注2:您可以使用memcpy来复制字符串(因为您知道长度并且不使用终端字符'\0')。
#1
3
Your second function is wrong. It should copy from src + i
not from src
. Also the nested loop should be done for the target_size
elements of dest
, not for src_size
as in your code. This results in:
你的第二个功能是错误的。它应该从src + i复制而不是src。还应该为target_size元素执行嵌套循环,而不是在代码中使用src_size。这将导致:
void compare2(char* src , char* target , int src_size , int target_size , int* score){
int i , j;
char* dest = (char*)malloc(sizeof(char)*(target_size));
for( i = 0 ; i < src_size - target_size ; i++){
strncpy(dest,src + i,target_size);
for( j = 0 ; j < target_size ; j++){
if(dest[j] == target[j]){
score[i]++;
}else{
continue;
}
}
}
free(dest);
}
Remark 1: the else continue;
is useless ! Remark 2: you can use memcpy
to copy the string (since you know the length and don't use the terminal character '\0'
).
注1:else继续;是无用的!备注2:您可以使用memcpy来复制字符串(因为您知道长度并且不使用终端字符'\0')。