#1 (permalink)  
Old 05-30-2008, 10:38 AM
curtiss's Avatar
Moderator
 
Join Date: May 2003
Posts: 1,445
PHP Code, Efficiency and Benchmarking

I'm curious if anyone out there has found any good, reliable ways to benchmark various PHP functions. At the moment, I'm looking at trying to compare two or three ways of achieving the same result, and I have no idea how to properly benchmark the two methods to see which is faster, more efficient and more reliable.

For those curious, I am working with data that will always be in the form of a string. That string will always begin with two letters (either "ff" or "fs"), followed by any number of digits. I have considered the following possibilities, but I have no idea which method would be the most efficient, or if it would even make a difference.

I have looked at:
PHP Code:
$val 'ff27'# An example of what my val might look like
if(strstr($val,'ff') !== false) {
$id substr($val,2,strlen($val)-2);
# Perform my code related to the ff value
}
elseif(
strstr($val,'fs') !== false) {
$id substr($val,2,strlen($val)-2);
# Perform my code related to the fs value

PHP Code:
$val 'ff27'# An example of what my val might look like
if(strstr($val,'ff') !== false) {
$id str_replace('ff','',$val);
# Perform my code related to the ff value
}
elseif(
strstr($val,'fs') !== false) {
$id str_replace('fs','',$val);
# Perform my code related to the fs value

PHP Code:
$val 'ff|27';
list(
$pre,$id) = explode('|',$val);
if(
$pre == 'ff') {
# Perform my code related to the ff value
}
elseif(
$pre == 'fs') {
# Perform my code related to the fs value

Anyone having any thoughts about the efficiency of any of these methods would be greatly appreciated.

In addition, if anyone has any good resources for comparing PHP code to check its efficiency (preferably a free method of doing so) would also be greatly appreciated. Thank you.
__________________
I hate Internet Explorer! Anyone with me?
Reply With Quote

  #2 (permalink)  
Old 05-30-2008, 01:09 PM
Deadeye's Avatar
Moderator
 
Join Date: Aug 2005
Location: San Diego, CA
Posts: 274
Send a message via MSN to Deadeye
unfortunately I don’t know of a way to benchmark code. But how often will this code be ran?

15x a sec
30x a sec
etc.

From exp I do know that substring is a slow way of doing things, the last way
Code:
$val = 'ff|27';
list($pre,$id) = explode('|',$val);
if($pre == 'ff') {
# Perform my code related to the ff value
}
elseif($pre == 'fs') {
# Perform my code related to the fs value
}
I believe is your best way of handling this problem. This is due to the fact any built in data type, i.e. list, will be able to perform the actions quicker.

I hope this helps in some way. I wish I had more data to back up my claims.

Wesley

*EDIT*
In C# I am able to measure how long a call takes by creating a new thread and a timer. Start the timer before the call to the code you want to measure and stop the timer after the call is finished. Display the time of the timer in nanoseconds and you'll get the time of the code.

I don't know if this is doable in PHP though
__________________
Don't forget to rate me A+++++++++++++++++!!!!!111one for the service you have received.

Last edited by Deadeye; 05-30-2008 at 04:39 PM.
Reply With Quote
  #3 (permalink)  
Old 06-02-2008, 06:42 PM
curtiss's Avatar
Moderator
 
Join Date: May 2003
Posts: 1,445
Thanks for the advice. It is appreciated.

I'll keep digging to see if I can find any way to actually test things whenever I run into a situation like this.

If I find anything, I'll let you guys know.
__________________
I hate Internet Explorer! Anyone with me?
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT -5. The time now is 12:41 PM.

 
Bitrix
Clicky Web Analytics
CloudContacts
Maxtango


Subscribe to our feed | add to myYahoo!

Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.2.0
© 1997-2007 HTMLCenter