Re: c / c++ : is it end of era ?
- From: jacob navia <jacob@xxxxxxxxxxxxxxxx>
- Date: Tue, 02 Jan 2007 00:38:29 +0100
Andrew Sidwell a écrit :
jacob navia wrote:
<snip>
In any case it proves my point
strchr is slower by more than 30 % compared to memchr under libc/linux
dougal(~/navia) cat chr.c
#include <stdio.h>
#include <string.h>
#define MAXITER 100000000
int main(void)
{
char s[4096];
int i;
memset(&s, 'a', sizeof(s) - 1);
s[sizeof(s)-1] = 0;
for (i = 0; i < MAXITER; i++)
{
#ifdef MEMCHR
memchr(s, '1', sizeof s)
#else
strchr(s, '1')
#endif
}
return 0;
}
dougal(~/navia) gcc -O0 chr.c -o strchr
dougal(~/navia) time ./strchr
real 0m1.403s
user 0m0.990s
sys 0m0.040s
dougal(~/navia) time ./strchr
real 0m1.743s
user 0m1.000s
sys 0m0.050s
dougal(~/navia) time ./strchr
real 0m1.212s
user 0m1.000s
sys 0m0.050s
dougal(~/navia) gcc -O0 chr.c -DMEMCHR -o memchr
dougal(~/navia) time ./memchr
real 0m1.874s
user 0m1.010s
sys 0m0.020s
dougal(~/navia) time ./memchr
real 0m1.485s
user 0m1.030s
sys 0m0.030s
dougal(~/navia) time ./memchr
real 0m1.282s
user 0m0.990s
sys 0m0.020s
I can see no clear pattern in the performance of the two functions, and
believe that your claim is unsubstantiated.
/proc/cpuinfo says that the processor this was run on was a 735Hz VIA
Ezra (x86) processor, with the flags "fpu de tsc msr cx8 mtrr pge mmx
3dnow", and a 64KB cache.
Andrew Sidwell
Obviously there is something wrong since in a MUCH faster machine
for 100 million iterations I take 84/43 SECONDS and you take not even
a second...
I explicitely said that you should NOT set any optimizations, and you
compiled with O0. O0 DOES some optimizations, so this is quite dangerous
with benchmarks. I would suggest that you use this program without any
optimizations at all:
#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[i] = '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;
}
The additions to the variable "c" disable any optimization
of the calls away. Besides, main returns that value
and this means that gcc must generate the code as told.
It is very disappointing that compilers optimize when told
not to!!!
jacob
.
- Follow-Ups:
- Re: c / c++ : is it end of era ?
- From: Dik T. Winter
- Re: c / c++ : is it end of era ?
- From: Andrew Sidwell
- Re: c / c++ : is it end of era ?
- From: Dik T. Winter
- Re: c / c++ : is it end of era ?
- References:
- Re: c / c++ : is it end of era ?
- From: Dave Vandervies
- Re: c / c++ : is it end of era ?
- From: jacob navia
- Re: c / c++ : is it end of era ?
- From: Andrew Sidwell
- Re: c / c++ : is it end of era ?
- Prev by Date: Getting a function to return an array
- Next by Date: Re: Getting a function to return an array
- Previous by thread: Re: c / c++ : is it end of era ?
- Next by thread: Re: c / c++ : is it end of era ?
- Index(es):
Relevant Pages
|