473,396 Members | 2,129 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,396 software developers and data experts.

Arrays + Performance surprise - curious for comments

Folks,
A ng poster recently questioned their usage/creation of arrays and their
correct syntax. I got the idea to performance test from a recent
(excellent) PHP Tutorial article that was in Linux Format magazine (which
dealt with performance).

The original poster had a reply from someone who had said using
$testArray[$keyName] was better (and proper) than $testArray["$keyName"]

I *had* believed this to be correct... but... against my better judgement, I
decided to test the assumption.

I did a simple test to confirm - Basically, a loop that performed a
conditional 'if' test a large number of times - One used the double quotes
around the array key, one went without. Other than that, the rest was the
same - the box was almost asleep too during the test with a single user and
no cron jobs or other activity. To make sure, I ran the script twice, ten
minutes apart.... and the results?

Strangely, the test *with* the double quotes took half the time. We're
talking about Test 1 taking 85seconds and test 2 taking 152seconds

I'm curious on anything anyone can add to this - It got me thinking that if
something is faster, is it right/recommended practice?

My test script is below....

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet?

<?
// Perform a test $maxLoop number of times, count the number of seconds
// it takes to complete the loop - Test 1 has the array element named
// inside double quotes, Test 2 excludes the double quotes
set_time_limit(250);
$testArray=$_ENV;
$maxLoop=100000;
/////////////////////// Test 1 //////////////////// This took 85seconds to
complete
$start=time();
for($loopMany=0; $loopMany<$maxLoop; ++$loopMany)
{ foreach($testArray as $elementName=>$elementValue)
{ if($testArray["$elementName"]=="HOST")
{ $thisHost="$elementValue"; }
}
}
$end=time();
$difference=$end-$start;
print("<br>Duration 1: $difference<hr>");

set_time_limit(250);
$testArray=$_ENV;
$maxLoop=100000;
/////////////////////// Test 2 //////////////////// This took 151 seconds to
complete
for($loopMany=0; $loopMany<$maxLoop; ++$loopMany)
{ foreach($testArray as $elementName=>$elementValue)
{ if($testArray[$elementName]=="HOST")
{ $thisHost="$elementValue"; }
}
}
$end=time();
$difference=$end-$start;
print("<br>Duration 2: $difference<hr>");
?>

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet?
Jul 17 '05 #1
3 2562

"Randell D." <yo**************************@yahoo.com> wrote in message
news:Br************************@news2.calgary.shaw .ca...
Folks,
A ng poster recently questioned their usage/creation of arrays and their
correct syntax. I got the idea to performance test from a recent
(excellent) PHP Tutorial article that was in Linux Format magazine (which
dealt with performance).

The original poster had a reply from someone who had said using
$testArray[$keyName] was better (and proper) than $testArray["$keyName"]

I *had* believed this to be correct... but... against my better judgement, I decided to test the assumption.

I did a simple test to confirm - Basically, a loop that performed a
conditional 'if' test a large number of times - One used the double quotes
around the array key, one went without. Other than that, the rest was the
same - the box was almost asleep too during the test with a single user and no cron jobs or other activity. To make sure, I ran the script twice, ten
minutes apart.... and the results?

Strangely, the test *with* the double quotes took half the time. We're
talking about Test 1 taking 85seconds and test 2 taking 152seconds

I'm curious on anything anyone can add to this - It got me thinking that if something is faster, is it right/recommended practice?

My test script is below....

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet?

<?
// Perform a test $maxLoop number of times, count the number of seconds
// it takes to complete the loop - Test 1 has the array element named
// inside double quotes, Test 2 excludes the double quotes
set_time_limit(250);
$testArray=$_ENV;
$maxLoop=100000;
/////////////////////// Test 1 //////////////////// This took 85seconds to complete
$start=time();
for($loopMany=0; $loopMany<$maxLoop; ++$loopMany)
{ foreach($testArray as $elementName=>$elementValue)
{ if($testArray["$elementName"]=="HOST")
{ $thisHost="$elementValue"; }
}
}
$end=time();
$difference=$end-$start;
print("<br>Duration 1: $difference<hr>");

set_time_limit(250);
$testArray=$_ENV;
$maxLoop=100000;
/////////////////////// Test 2 //////////////////// This took 151 seconds to complete
for($loopMany=0; $loopMany<$maxLoop; ++$loopMany)
{ foreach($testArray as $elementName=>$elementValue)
{ if($testArray[$elementName]=="HOST")
{ $thisHost="$elementValue"; }
}
}
$end=time();
$difference=$end-$start;
print("<br>Duration 2: $difference<hr>");
?>

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet?


As 'BKDotCom' points out in another post - I got something wrong in my test
8(...

*cough cough*
Your test 2 took 66 seconds.
you didn't reinitialize $start after test1 compleated... therefore
152 seconds is the total time your script ran.

so:
86 sec for test 1 (quotes)
66 sec for test 2 (no quotes)
152 sec: total

but, bonus points to you for actually taking the time to research this.
Jul 17 '05 #2
I tried following:

<?php
$maxLoop=100000;
$iterations=20;

$start=time();
for ($i=0;$i<$iterations;$i++){
unset($test);
for($loopMany=0; $loopMany<$maxLoop; ++$loopMany)
{
$test[]=1;
}
}
$end=time();
$difference=$end-$start;
print("<br>Duration 0: $difference<hr>");

$start=time();
for ($i=0;$i<$iterations;$i++){
unset($test);
for($loopMany=0; $loopMany<$maxLoop; ++$loopMany)
{
$test[$loopMany]=1;
}
}
$end=time();
$difference=$end-$start;
print("<br>Duration 1: $difference<hr>");

$start=time();
set_time_limit(250);
for ($i=0;$i<$iterations;$i++){
unset($test);
for($loopMany=0; $loopMany<$maxLoop; ++$loopMany)
{
$test["$loopMany"]=1;
}
}
$end=time();
$difference=$end-$start;
print("<br>Duration 2: $difference<hr>");
?>

and got these results:

$test[] 18secs
$test[$loopMany] 22secs
$test["$loopMany"] 50secs

I think the difference is due to how PHP handles quoted strings. PHP
first has to parse the quoted string and replace all variables with
their values (probably creates a temp string for this operation) and
after that it can use the resulted string as array index. When
variable is used directly PHP doesn't have to do this extra step.
But I'm only guessing, I have no idea of the inner workings of PHP.

- allan savolainen
Jul 17 '05 #3
Allan Savolainen wrote:

I think the difference is due to how PHP handles quoted strings. PHP
first has to parse the quoted string and replace all variables with
their values (probably creates a temp string for this operation) and
after that it can use the resulted string as array index. When
variable is used directly PHP doesn't have to do this extra step.
But I'm only guessing, I have no idea of the inner workings of PHP.

- allan savolainen


Allan,

You're exactly right. Putting a variable in double-quotes is called
string interpolation. It not only requires this extra step, but calls
up a whole subset of parsing routines to break the string into
constituent parts. (In some instances, it's even faster to concatenate
static strings instead of relying on interpolation.) You should also
run your test using all of the different quoting methods:

$mykey = 'key';
$array[]
$array[key]
$array['key']
$array["key"]
$array[$mykey]
$array["$mykey"]

[Additional Tests:]
$k1 = 'k'; $k2 = 'e'; $k3 = 'y';
$array["$k1$k2$k3"]
$array[$k1.$k2.$k3]
$array['k'.'e'.'y']

You should find out that using single-quoted strings are slightly faster
than the same double-quoted string. (It doesn't trip the interpolation
system, but just assumes the string is a constant.)

It's not all that hard to figure out what PHP is doing. The Zend
language is pretty similar to C, so you should be able to browse the
source code to PHP and get a good idea of "the inner workings of PHP."
I do all the time just to figure out what the best way to do something
is. At first, finding the code can be a little daunting (there's a
couple hundred source files). But, a good text searching program will
go a long way in finding the information you need. It might also help
to do a little research on the Apache API, since PHP is pretty
integrated into Apache for certain things (especially when you're using
it as a module and not as a CGI binary).

Does anybody know if heredocs are faster or slower than quoted strings?

Take care,
Zac

Jul 17 '05 #4

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

Similar topics

25
by: Brian Patterson | last post by:
I have noticed in the book of words that hasattr works by calling getattr and raising an exception if no such attribute exists. If I need the value in any case, am I better off using getattr...
10
by: KN | last post by:
I know both are pretty much the same and it comes down to personal choice. But I have to make the choice for the team. Things so far that I am considering 1. XML documentation in C# -- thats...
12
by: Dave Theese | last post by:
Hello all, I'm in a poition of trying to justify use of the STL from a performance perspective. As a starting point, can anyone cite any benchmarks comparing vectors to plain old...
1
by: James dean | last post by:
I done a test and i really do not know the reason why a jagged array who has the same number of elements as a multidimensional array is faster here is my test. I assign a value and do a small...
2
by: Michael Fitzpatrick | last post by:
I have a DLL written in C. This DLL reads a text file and creates a several very large arrays, 500,000 points and even larger. I would like the get the data in VB.Net so that I can plot it....
36
by: mrby | last post by:
Hi, Does anyone know of any link which describes the (relative) performance of all kinds of C operations? e.g: how fast is "add" comparing with "multiplication" on a typical machine. Thanks!...
3
by: yonil | last post by:
Over the years of using C++ I've begun noticing that freestore management functions (malloc/free) become performance bottlenecks in complex object-oriented libraries. This is usually because these...
1
by: Erland Sommarskog | last post by:
I am glad to announce that there is now a version of my article "Arrays and Lists in SQL Server" for SQL 2005 available on my web site. THe URL for the article is...
5
by: =?Utf-8?B?TWFyaw==?= | last post by:
Hi... I've been trying to improve the performance of our xml handling, so I've been doing some timing tests lately and ran across something I thought odd - XPathDocument seems slightly slower...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.