J
jacob navia
Randy Howard a écrit :
What I wanted to prove is just that since strchr must test
TWO characters: zero AND the character searched, it must be
slower than memchr that tests only if the count is exhausted.
Counted strings then, can use a memchr function to search
for a character since the count is known. strchr is much faster
then for those strings.
PHEW!!!!
What I thought would be evident for everyone was kind
of difficult to prove, specially since gcc seems to optimize
things when nobody is asking for it.
I personally think that a compiler should TRANSLATE the
program AS IS, not be some kind of artificial intelligence
machine that decide how you should have written the
program... specially because they guess WRONG in
some cases.
DAMM!!!!!
I sent you the wrong one.
Here is the one with the necessary corrections for gcc.
1) new variable c, is incremented at
each function call. Then the compiler
cab't optimize the call away.
To avoid that the variable is discarded
main returns that value
jacob navia a écrit :
I have sent you elsethread the corrected program.
Since now gcc optimizes even when it is not told
to do that, you have to trick it into generating code
for the program you wrote, not for the program gcc
think you should have written.
#include <stdio.h>
#include <time.h>
#include <string.h>
#define MAXITER 10000000
int main(void){
char s[4096];
int i,c=0;
time_t t,tStrchr,tMemchr;
for (i=0; i<sizeof(s)-1;i++) {
s = 'a';
}
s[sizeof(s)-1] = 0;
t = time(NULL);
for (i=0; i<MAXITER;i++) {
if (strchr(s,'1'))
c++;
;
}
tStrchr= time(NULL) - t;
printf("Time for strchr=%d\n",tStrchr);
t = time(NULL);
for (i=0; i<MAXITER;i++) {
if( memchr(s,'1',sizeof(s)))
c++;
}
tMemchr=time(NULL)-t;
printf("Time for memchr=%d\n",tMemchr);
return c;
Again, I had to reduce the MAXITER value by dividing it by 10 to get
reasonable run times.
Anyway, here is the output:
Time for strchr=17
Time for memchr=13
Okay, you managed to make gcc on the PPC G5 hate them both now. I'm
not sure what that is supposed to prove. Are we to assume people care
about best performance when compiling with optimization turned off?
What I wanted to prove is just that since strchr must test
TWO characters: zero AND the character searched, it must be
slower than memchr that tests only if the count is exhausted.
Counted strings then, can use a memchr function to search
for a character since the count is known. strchr is much faster
then for those strings.
PHEW!!!!
What I thought would be evident for everyone was kind
of difficult to prove, specially since gcc seems to optimize
things when nobody is asking for it.
I personally think that a compiler should TRANSLATE the
program AS IS, not be some kind of artificial intelligence
machine that decide how you should have written the
program... specially because they guess WRONG in
some cases.