473,386 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

.= very slow !

Hi,

I discovered something which surprised me today:

version 1/
$toto = "";
while (mysql->fetchrow) (about 400 times)
{
$toto .= "row1 row2 row3";
}

=> took 60s

version 2/
array $toto;
while (mysql->fetchrow) (about 400 times)
{
$toto[] = "row1 row2 row3";
}
is veryyyy faster ... 3s !

I discovered a difference with perl too but version 1 takes 8s only
Jul 17 '05 #1
4 1760
Thats why I never use that method... easier to read, nicer code... does not
perform very well..
"Pascal H" <mm*@mmm.com> wrote in message
news:40*********************@news.free.fr...
Hi,

I discovered something which surprised me today:

version 1/
$toto = "";
while (mysql->fetchrow) (about 400 times)
{
$toto .= "row1 row2 row3";
}

=> took 60s

version 2/
array $toto;
while (mysql->fetchrow) (about 400 times)
{
$toto[] = "row1 row2 row3";
}
is veryyyy faster ... 3s !

I discovered a difference with perl too but version 1 takes 8s only

Jul 17 '05 #2
"Pascal H" <mm*@mmm.com> wrote in
news:40*********************@news.free.fr:
I discovered something which surprised me today:

version 1/
$toto = "";
while (mysql->fetchrow) (about 400 times)
{
$toto .= "row1 row2 row3";
}

=> took 60s

version 2/
array $toto;
while (mysql->fetchrow) (about 400 times)
{
$toto[] = "row1 row2 row3";
}
is veryyyy faster ... 3s !

I discovered a difference with perl too but version 1 takes 8s only


String concatenation will generally be slower than adding an element to an
array because it's usually implemented by allocating a new block of memory
big enough to hold both strings, copying the original string over to it,
and then copying the new string to it. Adding an element to an array is
mostly just a matter of allocating memory for the new string and copying
it; the original contents of the array don't have to be copied.

This description is obviously oversimplified, but the main point is that
when you're concatenating, the original string has to be copied, and the
time this takes increases with each successive concatenation since the
original string is getting longer.

To get a little mathematical and use an oversimplified example (namely
dealing with N strings, all of the same length M characters), adding them
to an array will require N*M characters to be copied, so the run time is
proportional to N. We say that it "runs in linear time."

With concatenation, the first string has to be copied N times, the second
string N-1 times, and so on until the last string has to be copied only
once. This means that (N+(N-1)+(N-2)+...+1)*M characters have to be
copied, which works out to (N**2+N)*M (using "N**2" to mean "N squared"),
so the run time is now proportional to the square of N. We say that it
"runs in quadratic time."

The result is that if you double the number of strings, the array-insertion
method will take twice as long, but the concatenation method will take 6
times as long.

Of course an interpreter can optimize things a bit, for example by
allocating extra memory for each string so that in some cases that
concatenated string can just be appended onto the end of the original, but
there's a limit to how much this can be done. The main problem is that in
realistic scenarios, the interpreter has no idea how big the next string to
concatenate is going to be.
Jul 17 '05 #3
Very good responce,

thanks :P

"Eric Bohlman" <eb******@earthlink.net> wrote in message
news:Xn*******************************@130.133.1.4 ...
"Pascal H" <mm*@mmm.com> wrote in
news:40*********************@news.free.fr:
I discovered something which surprised me today:

version 1/
$toto = "";
while (mysql->fetchrow) (about 400 times)
{
$toto .= "row1 row2 row3";
}

=> took 60s

version 2/
array $toto;
while (mysql->fetchrow) (about 400 times)
{
$toto[] = "row1 row2 row3";
}
is veryyyy faster ... 3s !

I discovered a difference with perl too but version 1 takes 8s only
String concatenation will generally be slower than adding an element to an
array because it's usually implemented by allocating a new block of memory
big enough to hold both strings, copying the original string over to it,
and then copying the new string to it. Adding an element to an array is
mostly just a matter of allocating memory for the new string and copying
it; the original contents of the array don't have to be copied.

This description is obviously oversimplified, but the main point is that
when you're concatenating, the original string has to be copied, and the
time this takes increases with each successive concatenation since the
original string is getting longer.

To get a little mathematical and use an oversimplified example (namely
dealing with N strings, all of the same length M characters), adding them
to an array will require N*M characters to be copied, so the run time is
proportional to N. We say that it "runs in linear time."

With concatenation, the first string has to be copied N times, the second
string N-1 times, and so on until the last string has to be copied only
once. This means that (N+(N-1)+(N-2)+...+1)*M characters have to be
copied, which works out to (N**2+N)*M (using "N**2" to mean "N squared"),
so the run time is now proportional to the square of N. We say that it
"runs in quadratic time."

The result is that if you double the number of strings, the

array-insertion method will take twice as long, but the concatenation method will take 6
times as long.

Of course an interpreter can optimize things a bit, for example by
allocating extra memory for each string so that in some cases that
concatenated string can just be appended onto the end of the original, but
there's a limit to how much this can be done. The main problem is that in
realistic scenarios, the interpreter has no idea how big the next string to concatenate is going to be.

Jul 17 '05 #4
"Pascal H" <mm*@mmm.com> wrote in message news:<40*********************@news.free.fr>...
Hi,

I discovered something which surprised me today:

version 1/
$toto = "";
while (mysql->fetchrow) (about 400 times)
{
$toto .= "row1 row2 row3";
}

=> took 60s

version 2/
array $toto;
while (mysql->fetchrow) (about 400 times)
{
$toto[] = "row1 row2 row3";
}
is veryyyy faster ... 3s !

I discovered a difference with perl too but version 1 takes 8s only


Yeah, but if you really want a string, you have to add in the time it
would take to implode() your array.

Try this:
$text = '';
for ($i=0; $i<10000; $i++) {
$text .= ' '.$i;
}
echo '<p>'.$text;

Versus this:

$array = array();
for ($i=0; $i<10000; $i++) {
$array[] = $i;
}
$text = implode(' ', $array);
echo '<p>'.$text;
Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: yawnmoth | last post by:
using the gethostbyname function seems to noticeably slow down pages. some of the comments in php.net's gethostbyname entry suggest using a version that caches the result, but those versions also...
8
by: Neil | last post by:
I have a very puzzling situation with a database. It's an Access 2000 mdb with a SQL 7 back end, with forms bound using ODBC linked tables. At our remote location (accessed via a T1 line) the time...
2
by: David | last post by:
Hi, We have an internal network of 3 users. Myself & one other currently have individual copies of the front-end MS Access forms and via our individual ODBC links we have used the: File > Get...
3
by: Jennyfer J Barco | last post by:
In my application I have a datagrid. The code calls a Stored procedure and brings like 200 records. I created a dataset and then a dataview to bind the results of the query to my grid using ...
50
by: diffuser78 | last post by:
I have just started to learn python. Some said that its slow. Can somebody pin point the issue. Thans
0
by: Pratchaya | last post by:
In my.cnf i add these lines ####### log-bin log-slow-queries = /var/log/mysqld-slow.log long_query_time=1 #######
2
by: mezise | last post by:
Posted by Pratchaya: ------------------------------------------------------ MySQL Slow Log ERROR In my.cnf i add these lines ####### log-bin log-slow-queries = /var/log/mysqld-slow.log
13
by: eighthman11 | last post by:
using Access 2003 and sql server version 8.0 Hey everyone. Created a text box where the user types in an Inventory number and it takes them to that inventory number on the contimuous form. The...
3
by: John | last post by:
Hi I have replaced an ms access app with its vb.net version at a client site. Now the clients keeps complaining about how slow the app response is. The complains they have are for example when...
10
by: penworthamnaynesh | last post by:
Does php slow your website down? This is what i would like to know.The reason is because my site is writtent 50% in html and 50% in php and is very slow at loading. And i cant tell wether php is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.