TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-12-2007, 12:54 PM   #1 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Angry 40 Tips for optimizing your php Code - Really?

I was reading this article the other week, but I've just come across it again. There are a few points I don't agree with, such as:
  1. 1: I was always told not to do this, PHP will find the fastest way of doing things, and making it static just for the sake of it is a big no no.
  2. 29: I wouldn't say echo is a function at all.
  3. 35: Not everything is about speed, is it?
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 12-12-2007, 01:21 PM   #2 (permalink)
bdm
The Acquainted
Good Samaritan 
 
Join Date: Nov 2007
Posts: 127
Thanks: 14
bdm is on a distinguished road
Default

Ah yes, I read that a while back.

I'd like to see some of the benchmarks he did. He seems to simply tell us that x is faster than y without some proof.

And no, not everything is about speed. I took a big chunk of code(without comments) few days ago and made it into a nifty function which is about half the size of the original block of code, use regular expressions to test certain conditions instead of using substr() on every string 5 times to check if certain sections of the string contained certain characters. Best of all, I added comments, and the readability is through the roof. Adding new checks to the function is dead easy, even Wildhoney could do it.
bdm is offline  
Reply With Quote
Old 12-12-2007, 02:15 PM   #3 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default

I remember skimming over that too, he has some good points, but he also has a lot of things I disagree with too. I think people get carried away with performance. I personally prefer to design for flexibility first, and worry about performance second; it's easier to improve performance than add flexibility.
__________________
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
Karl is offline  
Reply With Quote
Old 12-12-2007, 03:26 PM   #4 (permalink)
Jay
The Contributor
Good Samaritan 
 
Join Date: Dec 2007
Posts: 60
Thanks: 5
Jay is on a distinguished road
Default

Quote:
Originally Posted by 40 tips
When working with strings and you need to check that the string is either of a certain length you'd understandably would want to use the strlen() function. This function is pretty quick since it's operation does not perform any calculation but merely return the already known length of a string available in the zval structure (internal C struct used to store variables in PHP). However because strlen() is a function it is still somewhat slow because the function call requires several operations such as lowercase & hashtable lookup followed by the execution of said function. In some instance you can improve the speed of your code by using an isset() trick.

Ex.
PHP Code:
if (strlen($foo) < 5) { echo "Foo is too short"; } 
vs.
PHP Code:
if (!isset($foo{5})) { echo "Foo is too short"; } 
I knew it!
Jay is offline  
Reply With Quote
Old 12-12-2007, 04:13 PM   #5 (permalink)
The Contributor
RegEx Guru 
 
Join Date: Dec 2007
Location: Belgium
Posts: 60
Thanks: 6
Geert is on a distinguished road
Default

Read it a while back. Not everything seems to be true. For example, point 11. According to my benchmarks str_replace is simple faster.

Number one rule about optimization is to benchmark. Then you know for sure. And of course, as Karl pointed out also, functionality comes at the first place.
__________________
Kohana - PHP5 framework
Geert is offline  
Reply With Quote
Old 12-13-2007, 03:26 AM   #6 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Good tips, a few things I will have to start doing
__________________

Village Idiot is offline  
Reply With Quote
Old 12-13-2007, 03:16 PM   #7 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

Same here, formatted my own list. Some things I don't agree with, and the others I don't understand.

I do not agree with;
1. If a method can be static, declare it static. Speed improvement is by a factor of 4;

I do not understand these points;
7. require_once() is expensive;
15. Turn on apache's mod_deflate.

And these I will use for sure;
10. See if you can use strncasecmp, strpbrk and stripos instead of regex;
14. Error suppression with @ is very slow;
17. $row[’id’] is 7 times faster than $row[id];
18. Error messages are expensive;
19. Do not use functions inside of for loop, such as for ($x=0; $x < count($array); $x) The count() function gets called each time;
30. A PHP script will be served at least 2-10 times slower than a static HTML page by Apache. Try to use more static HTML pages and fewer scripts. (did know it, now I understand why);
31. Your PHP scripts are recompiled every time unless the scripts are cached. Install a PHP caching product to typically increase performance by 25-100% by removing compile times (like which program?);
32. Cache as much as possible. Use memcached - memcached is a high-performance memory object caching system intended to speed up dynamic web applications by alleviating database load. OP code caches are useful so that your script does not have to be compiled on every request;
33.
PHP Code:
if (strlen($foo) < 5) { echo "Foo is too short"; }
vs.
if (!isset(
$foo{5})) { echo "Foo is too short"; } 
Not everything has to be OOP, often it is too much overhead, each method and object call consumes a lot of memory.
I LOVE YOU.
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 12-14-2007, 08:05 PM   #8 (permalink)
The Wanderer
 
Join Date: Dec 2007
Posts: 14
Thanks: 0
gilzow is on a distinguished road
Default

Quote:
Originally Posted by ReSpawN View Post
I do not understand these points;
7. require_once() is expensive;
I believe this is due to the overhead in having to see if the file in question has already been required. I would bet that include_once() is also expensive for the same reasons.
gilzow is offline  
Reply With Quote
Old 12-14-2007, 08:16 PM   #9 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

I think you're spot on with that, Gilzow. Although I don't imagine it adds much overhead at all - probably a few milliseconds in it, I would assume, without profiling it.

After all, how much would include differ from include_once? All it needs to do is check if the file has already been SSI'd - and it all depends how it goes about that. I imagine with a call to an internal log of some kind which would be rather speedy.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 12-14-2007, 08:36 PM   #10 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

i agree with Karl, i cant be doing with sacrificing maintainable flexable code for the extra 0.0000000000000000000001 of performance, obviously you do need to think about performance issues but alot of it comes down to common sense i.e. calling count inside the for loop:
PHP Code:
for ($x=0$x count($array); $x
its just plain common sense, if you dont realise whats going on there and why it can potentially cause problems, just think about it either that rethink your life as a PHP programmer.

People do get carried away with themselves and come up with some retarded things that are barely readable but it saves that all important ms, winds me up, these are the same people who seem to be allergic to OOP, just because it can use more resources, done properly its not that much of a difference and its also maintainable and extend able so that you don't need to re produce code all the time, these days time of development is more costly than the hardware, but anyway ill stop myself there its another issue i have a bee in my bonnet about.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 12-14-2007, 08:42 PM   #11 (permalink)
The Wanderer
 
Join Date: Dec 2007
Posts: 14
Thanks: 0
gilzow is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
I think you're spot on with that, Gilzow. Although I don't imagine it adds much overhead at all - probably a few milliseconds in it, I would assume, without profiling it.
agreed. I'm betting that the difference between the two is minuscule, at least in most situations. I bet you begin to see a bigger difference in situations where you have a LOT of required/included files.

Funny you mention the profiling, cuz that's just what I did. I had one file that required 5 files, and a second that did a require_once for each of the 5 files. The require_once file took 44.3 milliseconds longer than the require version.
gilzow is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 04:12 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design