Article View: borland.public.cppbuilder.nativeapi
Article #2116Re: Extremely slow string functions
From: 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.
Technical Details
Message-ID:
<m2wt0pli2a.fsf@glue.ch>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!proxad.net!213.200.89.82.MISMATCH!tiscali!newsfeed1.ip.tiscali.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<46161b72@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,cb6147be69a44149
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Re: Extremely slow string functions
References: <46161b72@newsgroups.borland.com>
From: maeder@glue.ch (Thomas Maeder [TeamB])
Organization: TeamB
X-Face: ":yn4zt^A_dn>6~{YJmu^B\'4w)e:apPM\VC=Dvf&*tASxX9|BYczkFd$xr.$vNSZ[3Uobr p]c4\+\l){!WQI.$]veP({HLd;CDYH?iN
Date: Fri, 06 Apr 2007 15:59:41 +0200
Message-ID: <m2wt0pli2a.fsf@glue.ch>
User-Agent: Gnus/5.1006 (Gnus v5.10.6) XEmacs/21.4 (Jumbo Shrimp, linux)
Cancel-Lock: sha1:Oggj07Jf7xQxEPLtZrxOnGk5a4k=
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
NNTP-Posting-Host: 84.73.227.225
X-Trace: newsgroups.borland.com 1175868002 84.73.227.225 (6 Apr 2007 07:00:02 -0800)
Lines: 55
X-Authenticated-User: tmaeder
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!proxad.net!213.200.89.82.MISMATCH!tiscali!newsfeed1.ip.tiscali.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail