News:

Wondering if this will always be free?  See why free is better.

Main Menu

Some benchmarks...

Started by [Unknown], September 15, 2005, 08:11:16 PM

Previous topic - Next topic

[Unknown]

Groundup really likes benchmarks.  Well, here you go... here are some.

These tests are all done on PHP 5.1.0RC2 but have also been done on previous versions, such as PHP 4.3.0 and PHP 5.0.5 in the past with similar results.

1. Why I don't like constants:

Comparing these two:

define('CONSTANT' . $GLOBALS['i'], $GLOBALS['x'] . $GLOBALS['y'] . $GLOBALS['z']);
$GLOBALS['i']++;

# ---

$GLOBALS['CONSTANT' . $GLOBALS['i']] = $GLOBALS['x'] . $GLOBALS['y'] . $GLOBALS['z'];
$GLOBALS['i']++;


Variable definition consitently takes 60% of the time constant definition does.  This is with PHP 5.1.0, but I noticed the same results on PHP 4.3.0 a while back.

This code:

$GLOBALS['test'] = CONSTANT + 1;

# ---

$GLOBALS['test'] = $GLOBALS['CONSTANT'] + 1;

(where, again, each has the same value.)

Consistently shows no difference between constants and variables.  Constants have many benefits (no chance of problems from register_globals) but other than that they seem slower.

2. Is eval really slow?  When misused, it can clearly be a security risk, but so can include.  Let's look:

include('supplement/dummy.inc');

# ---

eval('?' . '>' . file_get_contents('supplement/dummy.inc'));


Consistently (without an accelerator, mind you!) they are shown to be approximately matched.

3. Is interpolation really slower?

echo $GLOBALS['x'], ' test ', $GLOBALS['y'], $GLOBALS['z'], ' test ';

# ---

echo "$GLOBALS[x] test $GLOBALS[y]$GLOBALS[z] test ";

(where said variables are fairly long.)

Consistently shows the first method (using commas) to take about 20% as long as interpolation.  Commas are a lot better.

4. Is foreach with $k => $v if you don't use $v slower than foreach over array_keys()?

foreach ($GLOBALS['dummy'] as $k => $dummy)
$k + 1;

# ---

$x = array_keys($GLOBALS['dummy']);
foreach ($x as $k)
$k + 1;


The first one (no array_keys() call) is consistently faster, taking 30% of the time.  It seems array_keys() is expensive, causing a second iteration.

5. Are minor notices really anything to worry about?

@$GLOBALS[dummy]++;

# ---

$GLOBALS['dummy']++;


Consistently, the one that doesn't generate a notice (constant dummy undefined, assumed 'dummy') takes 30% of the time.  That's a pretty big hit for two missing quotes.

Hope that was interesting.

-[Unknown]

forsakenlad

Thanks for the benchmark it really is helpful...

Quote from: [Unknown] on September 15, 2005, 08:11:16 PM
2. Is eval really slow?  When misused, it can clearly be a security risk, but so can include.  Let's look:

include('supplement/dummy.inc');

# ---

eval('?' . '>' . file_get_contents('supplement/dummy.inc'));


Consistently (without an accelerator, mind you!) they are shown to be approximately matched.

I use eval a lot, on a server that has accelerator installed, would it make a speed difference? You have also stated that there can be a security risk. Can you explain it a little more? Thanks again for the benchmark ;)
Eren "forsakenlad" Yaşarkurt
SMF Friend & Former Team Member

Joshua Dickerson

Why do you use eval a lot? Even if it were much faster than create_function(), it is still slower than running a precompiled bit of code. The security risk is that you eval() to run any bit of code. If you aren't well aware of your variables, those varialbes can do damage.
Come work with me at Promenade Group



Need help? See the wiki. Want to help SMF? See the wiki!

Did you know you can help develop SMF? See us on Github.

How have you bettered the world today?

Advertisement: