Thread View: borland.public.cppbuilder.nativeapi
8 messages
8 total messages
Started by Julek
Fri, 06 Apr 2007 10:05
Extremely slow string functions
Author: Julek
Date: Fri, 06 Apr 2007 10:05
Date: Fri, 06 Apr 2007 10:05
63 lines
2574 bytes
2574 bytes
Hello,
take a look at this program:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <time.h>
int WezR()
{
return (int)rand()*(int)rand()+(int)rand();
}
int main(int argc, char* argv[])
{
time_t TimeX,TimeX2;
time(&TimeX);
int D=10000000;
char *TTT=new char[D];
for(int a=0;a<D-1;++a)
TTT[a]='a';
for(int a=0;a<1000000;++a)
TTT[WezR()%(D-1)]='b';
TTT[D-1]=0;
time(&TimeX2);
printf("Preparation - %d\n",TimeX2-TimeX);
for(int a=0;a<1000;++a)
{
int NumerStart=WezR()%(D-1);
char *Start=TTT+NumerStart;
char *Stop=strstr(Start,"aba");
int Dl=(int)Stop-(int)Start;
int f=555;
}
time(&TimeX);
printf("Finding - %d\n",TimeX-TimeX2);
return 0;
}
It creates a large table. 90% of chars are 'a', 10% are 'b', in random
places.
Last "for" finds random place in this string, and looks for the next "aba"
occurence - usually it is about 10 chars from the starting point.
When I compile it with Visual Studio 2005, it shows:
Preparation - 0 or 1
Finding - 0
Even, when I change in the last "for" - 1000 to 10000, finding is still 0
seconds.
In C++ Builder, when I create new console project and run this program, it
shows:
Preparation - 2
Finding - 14
So strstr in C++ Builder is more than 200 times slower in this case. I
think, that C++ Builder is so slow, because this string is so large.
My Turbo C++ is "a little broken" - when I create new console project, I
can't link it - it shows "unresolved externals" about CodeGuard files. I
have to enable CG in project options. In release, program is slow too.
What is the reason? Do you have similar problem?
Re: Extremely slow string functions
Author: Eelke
Date: Fri, 06 Apr 2007 12:16
Date: Fri, 06 Apr 2007 12:16
12 lines
1546 bytes
1546 bytes
> So strstr in C++ Builder is more than 200 times slower in this case. I > think, that C++ Builder is so slow, because this string is so large. > My Turbo C++ is "a little broken" - when I create new console project, I > can't link it - it shows "unresolved externals" about CodeGuard files. I > have to enable CG in project options. In release, program is slow too. > What is the reason? Do you have similar problem? > The reason is CodeGaurd. Codegaurd checks all your memory accesses which is slowing down the program. Try to get it to link without codeguard. Eelke
Re: Extremely slow string functions
Author: Thomas Maeder [TeamB]
Date: Fri, 06 Apr 2007 13:59
Date: Fri, 06 Apr 2007 13:59
55 lines
2256 bytes
2256 bytes
"Julek" <julekmen@go2.pl> writes:
> take a look at this program:
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <limits.h>
> #include <string.h>
> #include <time.h>
>
> int WezR()
> {
> return (int)rand()*(int)rand()+(int)rand();
The return type of rand() already is int. Why the casts?
More importantly, the expression may overflow, in which case your
program has undefined behavior.
In particular, WezR() may very well return a negative value ...
> }
>
> int main(int argc, char* argv[])
> {
> time_t TimeX,TimeX2;
> time(&TimeX);
> int D=10000000;
> char *TTT=new char[D];
>
> for(int a=0;a<D-1;++a)
> TTT[a]='a';
> for(int a=0;a<1000000;++a)
[make D const and change 1000000 to D/10, to err on the safe side]
> TTT[WezR()%(D-1)]='b';
... and applying % doesn't make it non-negative; more undefined
behavior ...
> TTT[D-1]=0;
> time(&TimeX2);
> printf("Preparation - %d\n",TimeX2-TimeX);
> for(int a=0;a<1000;++a)
> {
> int NumerStart=WezR()%(D-1);
> char *Start=TTT+NumerStart;
... and more here. This is where I stopped looking.
Try fixing your program and report back.
Re: Extremely slow string functions
Author: Minas
Date: Fri, 06 Apr 2007 14:08
Date: Fri, 06 Apr 2007 14:08
20 lines
1168 bytes
1168 bytes
"Julek" <julekmen@go2.pl> wrote in msg news:46161b72@newsgroups.borland.com... > int D=10000000; > char *TTT=new char[D]; You have to delete array TTT delete[] TTT; I got Preparation -0 or 1 finding - 0 _minas ------ "Only the virtue's conquests have certainty" Sofokleous Erephyle
Re: Extremely slow string functions
Author: Bob Gonder
Date: Fri, 06 Apr 2007 14:56
Date: Fri, 06 Apr 2007 14:56
20 lines
1577 bytes
1577 bytes
Thomas Maeder [TeamB] wrote: >> #include <stdlib.h> >> return (int)rand()*(int)rand()+(int)rand(); > >More importantly, the expression may overflow, in which case your >program has undefined behavior. Ummm, I think the maximum calculated value is 3FFF 8000, which is positive, and doesn't overflow unless int is 16bit. Replace rand() with RAND_MAX and you get RAND_MAX * RAND_MAX + RAND_MAX For easier manual calculation: RAND_MAX * (RAND_MAX+1) 7FFF * 8000 = 3FFF8000 Perhaps you were thinking of _lrand which returns a 31 bit value.
Re: Extremely slow string functions
Author: Koen
Date: Fri, 06 Apr 2007 15:00
Date: Fri, 06 Apr 2007 15:00
5 lines
946 bytes
946 bytes
It must be the CodeGuard, my output of the program is, even when I change the last for-loop to 100000 iterations: Preparation - 0 Finding - 0
Re: Extremely slow string functions
Author: Thomas Maeder [TeamB]
Date: Fri, 06 Apr 2007 16:12
Date: Fri, 06 Apr 2007 16:12
16 lines
1594 bytes
1594 bytes
Bob Gonder <notbg@notmindspring.invalid> writes: > Thomas Maeder [TeamB] wrote: > >>> #include <stdlib.h> > >>> return (int)rand()*(int)rand()+(int)rand(); >> >>More importantly, the expression may overflow, in which case your >>program has undefined behavior. > > Ummm, I think the maximum calculated value is 3FFF 8000, which is > positive, and doesn't overflow unless int is 16bit. RAND_MAX is an implementation-defined value. The Standard doesn't guarantee that the quoted expression doesn't overflow.
Re: Extremely slow string functions
Author: Julek
Date: Fri, 06 Apr 2007 21:28
Date: Fri, 06 Apr 2007 21:28
6 lines
1132 bytes
1132 bytes
U�ytkownik "Koen" <koen@ha.com> napisa� w wiadomo�ci news:Xns990AA2E742888koenhacom@207.105.83.66... > It must be the CodeGuard, Yes, it's the real reason :)Thank you.
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads