Suffered the most bizarre thing today: a change of function names several times due to differences in operational efficiency!

Category: C/C++ -> C++ Author: xiaowanzi19870530 Date: 2003-11-30 20:03:45
 
xiaowanzi19870530
2003-11-30 20:03:45
The whole procedure is as follows, where strncmp () library function with a source code, we can directly compiled into an executable file to run it, as long as two seconds even finished.
but the strange part is that, when the strncmp () changed to any other name, it was found immediately becomes very slow speed, to be considered finished more than a minute.

too strange, it will affect the speed of even the name of the function you? Speechless. . .

# include <stdio.h>
# include <stdlib.h>
# include <time.h>

# undef strncmp
int strncmp (const char *, const char *, size_t);
void print_time ();

main ()
{
char * s1, * s2;
long i;

s1 = (char *) malloc ( 40);
s2 = (char *) malloc (40);
s1 = "abcdabcdabcd";
s2 = "abcdabcdabcdabcdabcdabcd" ;;
print_time ();
for (i = 0; i <100000000; i + +)
strncmp (s1, s2, 20);
print_time ();
}

int
strncmp (s1, s2, n)
const char * s1;
const char * s2;
size_t n;
{
; unsigned char c1 = '\ 0';
unsigned char c2 = '\ 0';

if ; (n> = 4)
{
size_t n4 = n> ;> 2;
do
{
c1 = (unsigned char) * s1 + +;
c2 = (unsigned char) * s2 + +;
if (c1 == '\ 0' | | c1! = c2)
return c2 - c1;
c1 = (unsigned char) ; * s1 + +;
c2 = (unsigned char) * s2 + +;
if (c1 == '\ 0' | | c1! = c2)
return c2 - c1;
c1 = (unsigned char ) * s1 + +;
c2 = (unsigned char) * s2 + +;
if (c1 == '\ 0' | | c1! = c2)
return c2 - c1;
c1 = (unsigned ; char) * s1 + +;
c2 = (unsigned char) * s2 + +;
if (c1 == '\ 0 '| | c1! = c2)
return c2 - c1;
} while (- n4 ;> 0);
n & = 3;
}

while (n> 0)
{
c1 = (unsigned char) * s1 + +;
c2 = (unsigned char) * s2 + +;
if (c1 == '\ 0' | | c1! = c2)
return c2 - c1;
n -;
}

return c2 - c1;
}

void print_time ()
{
time_t ltime;
ltime = time (NULL);
printf ("% s", ctime (& ltime));
}
cfy_blueneo
2003-11-30 20:06:33
so strange? Strncmp generally do not use such names.
after all libraries have such a name.
adwardswk
2003-11-30 20:19:54
Is there anything strange, do you think
# undef strncmp
this line is doing? This brings to undefined library function strncmp the?
it is not a macro, how can be undef, the answer is:
use this function name when calling a library function, no problem,
When this into other name, call is your definition of the function of,
feel you can not be a function of c library functions than it should efficiently,
hope you can accept this conclusion.
jummysanny
2003-11-30 20:24:39

agree
frank0904
2003-11-30 20:30:46
Oh you use is seemingly the strncmp glibc 2.7 source it
hbwjyxg
2003-11-30 20:47:11


however:
(1) I listed above strncmp () not to write, but the GNU standard library function of the source code (which is used by GCC).
(2) However, in order and the system comes with the library function separately, I put strncmp () inside all the c1-c2 are changed c2-c1, so that the results obtained opposite sign . This means that if get is 97 instead of -97, then that there's a strncmp () rather than the system comes with strncmp () operation is the result.
oooo12a
2003-11-30 20:59:13

I came to apologize, yesterday I just compile and run a bit on the blind to the conclusion,
then I hide a statement function name, find the link error, it can not find function,
added std :: domain analysis and remains so, I knew I was wrong,
but then the network is broken, life and death up the mountain, came home from work today, hastened to apologize.
for my own arbitrary, ignorance and shallow paid the price.

Incidentally, etc. The correct answer, I really do not know how to continue to analyze this problem.
sunchunhang2
2003-11-30 21:12:08
I'm sorry, I think you not only changed the name only ...

f beginning
t179098484
2003-11-30 21:37:13
my red hat 9 tried it, seemingly are 10 seconds, no difference, and there do not have that # undef strncmp does not matter.
either LZ in detail describe your environment.