performance - C time difference between two functions -


I have two simple code functions that compare two strings.

The strategy is to compare a short string with a long string respectively.

For example,

  string1 = "ABCDABCDEFGAHAD", string 2 = "ABCD"  

then compare By the end of string1 and string2 by

  zero compare1 (char * src, char * target, int src_size, int target_size, int * score) has been used I) {int i, j; For (i = 0; i & lt; src_size - target_size; i ++) {for (j = 0; j & lt; target_size; j ++) {if (src [i + j] == target ]) {Score [i] ++; } And {continue; }}}}  

and the other

  zero compare2 (char * src, char * target, int src_size, int target_size, int * score)) {Int i, j; Char * dest = (four *) molk (size) * (target_size)); {Strncpy (dest, src, target_size) for (i = 0; i & lt; src_size - target_size; i ++); (J = 0; j and lt; src_size; j ++) {if (dest [j] == target [j]) {scores [i] ++; } And {continue; }}} Free (dest);  

These two give me different times, which compare

  8393.00 ms compare 4415.00 ms compare 2  

What does this time difference do?

The length of the two strings is in relation to the src and the target in 1024 * 1024 * 10 and 128 forum variables.

Your second function is false I would call it src + i to Src should not be copied. In addition, the nested loop should be done for target_size elements of dest , not as src_size as your code. The result of this is:

  zero compare2 (char * src, char * target, int src_size, int target_size, int * score) {int i, j; Char * dest = (four *) molk (size) * (target_size)); {Strncpy (dest, src + i, target_size) for (i = 0; i & lt; src_size - target_size; i ++); For (j = 0; j and lt; target_size; j ++) {if (dest [j] == target [j]) {scores [i] ++; } And {continue; }}} Free (dest); }  

Note 1: and continue; is useless! Note 2: You can use memcpy to copy the string (since you know the length and do not use the terminal character '\ 0' ).


Comments

Popular posts from this blog

winforms - C# Form - Property Change -

java - Algorithm negotiation fail SSH in Jenkins -

java - Messages from .properties file do not display UTF-8 characters -