Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 10-17-2007, 10:15 AM   #1 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,475
sde is on a distinguished road
40 Tips for optimizing your php Code

I'm not sure how accurate his testing was but it's a great read none the less.

reinholdweber.com
__________________
Mike
sde is offline   Reply With Quote
Old 10-17-2007, 01:53 PM   #2 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,151
Belisarius is on a distinguished road
Damn - I'm doing something on the order of 90% of the slow things he mentions. I didn't think "require_once" would be expensive - shouldn't it just be like a hash-table lookup or something?

They did leave out the big one that I came across - is_dir() is a *really* expensive call. I still use it all over the place, but I try really hard to not use it in large loops.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 10-18-2007, 12:17 AM   #3 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 661
DJMaze is on a distinguished road
#8: Use full paths in includes and requires, less time spent on resolving the OS paths.

This is wrong. In PHP5 when you use full paths each directory gets checked for permissions.
It makes any disk access function 4 times slower then in PHP 4 (per request around 0.0016 seconds).

#9: $_SERVER[’REQUEST_TIME’]
This is inaccurate and does not contain the script execution but the REQUEST

#17 $row[’id’] is 7 times faster than $row[id]
That's because $row[id] is bad code. Use error_reporting(E_ALL) and you will notice that PHP tries to find a constant named id.

The rest is rather true, yet some points sound like being the same as a previous point.
__________________

UT: Ultra-kill... God like!
DJMaze is offline   Reply With Quote
Old 11-03-2007, 02:49 PM   #4 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 661
DJMaze is on a distinguished road
Funny, i've just tested something with a fellow coder.
It seems using ternary operators in PHP is slower then binary if {} else {}

PHP Code:
<?php

$time 
microtime(true);
for (
$i=0$i<1000000; ++$i)
{
    
$foo = ($i%2==0) ? 1;
}
$time microtime(true)-$time;
echo 
'ternary in: '.round($time,4).' seconds<br>';

$time microtime(true);
for (
$i=0$i<1000000; ++$i)
{
    if (
$i%2==0)
    {
        
$foo 0;
    }
    else
    {
        
$foo 1;
    }
}
$time microtime(true)-$time;
echo 
'if..else.. in: '.round($time,4).' seconds<br>';
Result P4 2.54
Code:
ternary in: 0.5543 seconds
if..else.. in: 0.5709 seconds
Result P2
Code:
ternary in: 3.4309 seconds
if..else.. in: 2.7495 seconds
It's hardly noticeable though since how often do you use the ternary?
__________________

UT: Ultra-kill... God like!
DJMaze is offline   Reply With Quote
Old 11-03-2007, 04:07 PM   #5 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,475
sde is on a distinguished road
Code:
ternary in: 0.3621 seconds
if..else.. in: 0.3642 seconds
this is what i got on my laptop intel core duo with php5
__________________
Mike
sde is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
open new browser code for php charlesming PHP 11 10-13-2007 10:46 AM
Help with PHP code conversion to PERL vahagn PHP 5 05-15-2007 08:09 PM
PHP 5.0.4 and 4.3.11 Released sde Code Newbie News 0 04-20-2005 10:56 AM
Simple PHP MySQL code: confused. easilyi Everything SQL ( MySQL, MSSQL, DB2, Postgre, Oracle, etc...) 4 10-24-2004 07:53 PM
PHP code for Login Screen KEEBOD PHP 2 07-15-2004 05:18 PM


All times are GMT -8. The time now is 10:31 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting