Article View: borland.public.cppbuilder.nativeapi
Article #2113Extremely slow string functions
From: 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?
Technical Details
Message-ID:
<46161b72@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!newsgate.cistron.nl!xs4all!news2.euro.net!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,cb6147be69a44149,start X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Julek" <julekmen@go2.pl> Newsgroups: borland.public.cppbuilder.nativeapi Subject: Extremely slow string functions Date: Fri, 6 Apr 2007 12:05:42 +0200 Lines: 65 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.3028 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3028 X-RFC2646: Format=Flowed; Original NNTP-Posting-Host: 212.14.61.19 Message-ID: <46161b72@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1175853938 212.14.61.19 (6 Apr 2007 03:05:38 -0800) Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!newsgate.cistron.nl!xs4all!news2.euro.net!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail