BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Extremely slow string functions

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Native API)
View previous topic :: View next topic  
Author Message
Julek
Guest





PostPosted: Fri Apr 06, 2007 3:05 pm    Post subject: Extremely slow string functions Reply with quote



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?
Back to top
Eelke
Guest





PostPosted: Fri Apr 06, 2007 5:16 pm    Post subject: Re: Extremely slow string functions Reply with quote



Quote:
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
Back to top
Thomas Maeder [TeamB]
Guest





PostPosted: Fri Apr 06, 2007 6:59 pm    Post subject: Re: Extremely slow string functions Reply with quote



"Julek" <julekmen (AT) go2 (DOT) pl> writes:

Quote:
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 ...


Quote:
}

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]


Quote:
TTT[WezR()%(D-1)]='b';

.... and applying % doesn't make it non-negative; more undefined
behavior ...


Quote:
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.
Back to top
Minas
Guest





PostPosted: Fri Apr 06, 2007 7:08 pm    Post subject: Re: Extremely slow string functions Reply with quote

"Julek" <julekmen (AT) go2 (DOT) pl> wrote in msg
news:46161b72 (AT) newsgroups (DOT) borland.com...

Quote:
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
Back to top
Bob Gonder
Guest





PostPosted: Fri Apr 06, 2007 7:56 pm    Post subject: Re: Extremely slow string functions Reply with quote

Thomas Maeder [TeamB] wrote:

Quote:
#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.
Back to top
Koen
Guest





PostPosted: Fri Apr 06, 2007 8:00 pm    Post subject: Re: Extremely slow string functions Reply with quote

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
Back to top
Thomas Maeder [TeamB]
Guest





PostPosted: Fri Apr 06, 2007 9:12 pm    Post subject: Re: Extremely slow string functions Reply with quote

Bob Gonder <notbg (AT) notmindspring (DOT) invalid> writes:

Quote:
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.
Back to top
Julek
Guest





PostPosted: Sat Apr 07, 2007 2:28 am    Post subject: Re: Extremely slow string functions Reply with quote

Użytkownik "Koen" <koen (AT) ha (DOT) com> napisał w wiadomości
news:Xns990AA2E742888koenhacom (AT) 207 (DOT) 105.83.66...
Quote:
It must be the CodeGuard,

Yes, it's the real reason :)Thank you.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Native API) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.