The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Accurate Speed Measurement of execution

Hi all,

I'm new to this big place, so please be gentle.
I know some basic PHP code and I'm learning more day by day by reading the forums and the Sitepoint Articles.
But I ran into a question which I can not find the answer to.

Sometimes you see people say that it is better to echo multiple strings with , instead of concatinatiing them with . It will be faster!
Or some articles on the net mention that one architecture is faster than the other. Now I would like to do some tests of my own in this area.
For example, which is faster:
- Complex Reg Expr in one preg_match
- multiple if and else with simple strpos

Every time I load the same php script I get a different load time.
And it differes alot, sometimes up to 50% regardless if I empty my cache or not.
So if I get that big difference in the same script, how can I compare it to an other script to see which method of coding is faster?

But after testing it gave the same problem.
And reading about it in the manuall, it seems that the TRUE flag just means to return a floating point and not a string seperated by a space.
So in theory that is the same code, but a shorter version.

But after testing it gave the same problem.
And reading about it in the manuall, it seems that the TRUE flag just means to return a floating point and not a string seperated by a space.
So in theory that is the same code, but a shorter version.

Well, its not unusual to have different execution time every time you execute the same script. It depends on many factors like server load, available memory at the moment or execution and also OS will probably read the php file from the hard drive much faster the second time.

Also if you function involves a database access, that's a whole different story.

I don't know why, but the first time you call microtime() in a script it introduces a lot of variance.

php is a managed language, where it handles memory allocation and garbage collection and many other things for you behind the scenes, when it deems suitable. This will mess with your ability to profile execution speed.

Like said by the others, you need to loop the operation many times to help get an average that is less affected by these external factors you cannot control.

PHP Code:

// always ignore the first value this function returns
microtime(true);

// this time its more consisntant
$time_start = microtime(true);

for ($i=0; $i<10000; $i++) {
// do something
}

$time_end = microtime(true);
$total = $time_end - $time_start;

This isn't an ideal test. A regular expression needs to be internally parsed and compiled into an execution plan. php obviously does this for you. One thing that's not obvious though is that php will cache this compiled execution plan internally for the duration of the script, making future calls of the exact same regex faster than the first call.

This severely limits your ability to benchmark the speed of a regex, because very often you only use a given regex a single time per script execution, and so you use the slowest call. So the test in a loop gives the regex an unfair advantage. Well, unless this specific regex will actually be called many times in your script.

I don't know why, but the first time you call microtime() in a script it introduces a lot of variance.

...

This isn't an ideal test. A regular expression needs to be internally parsed and compiled into an execution plan. php obviously does this for you. One thing that's not obvious though is that php will cache this compiled execution plan internally for the duration of the script, making future calls of the exact same regex faster than the first call.

This severely limits your ability to benchmark the speed of a regex, because very often you only use a given regex a single time per script execution, and so you use the slowest call. So the test in a loop gives the regex an unfair advantage. Well, unless this specific regex will actually be called many times in your script.

Can I ask where you can find this kind of info? In a particular book? Experiance? A site maybe? This is really interesting!

Originally Posted by Cups

If you are truly fascinated by this kind of thing, then you should install xDebug, and find out about cache.grind files.

I Already had xDebug installed to improve my notepad ++ But I never heard of thos cache.grind files. I hadn't enabled it yet. I have done now and looking at them with wincache. Thanks for the tip, it is what I needed!

The regex cache I thought about after seeing in other languages that you could compile a regex and store it in a variable, for when you needed to execute it many times. I wondered why php didn't have such a thing, but then looked and saw it actually did it behind the scenes for us http://www.php.net/manual/en/intro.pcre.php

The regex cache I thought about after seeing in other languages that you could compile a regex and store it in a variable, for when you needed to execute it many times. I wondered why php didn't have such a thing, but then looked and saw it actually did it behind the scenes for us

So basicly experiance Thank you for sharing that with me
I still have a long way to go!