Confusion about PHP performance

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
bugrush
Forum Newbie
Posts: 15
Joined: Tue Dec 16, 2008 3:00 pm

Confusion about PHP performance

Post by bugrush »

Hi!

I want to ask you about php performance. Not that I have any problems running my php scripts. I'm perfectionist and I just can't sleep if some things are not clear to me.

Does parsed and compiled php code get cached somewhere or the server has to parse the unchanged code over and over again for each request? How important it is to separate the code with includes to eliminate all unnecessary code?

The way I write php right now is to have 2-5 includes with helper functions (database, url functions, some functions for string cleansing and error handler) which are used on almost every request. The rest of the code is included as needed. I usually have about 50-200 php files in my projects. Each request needs only 1-5 small files.

Also I don't use OOP in php. I use includes, functions and if/else statements are the paths to the right includes. I never had a situation where I have felt that I should use OOP. I don't have any "objects" in my code. I have only one instance of an "object" usually so the underscore character can replace all "->"'s. I don't care about restricting variable scopes either. It's my all my code. Arrays and functions are my friends. If I had a class with 1000 lines of code, I would still need only 1-2 member functions of a class on a single request. It's not a program that is loaded into the memory once on a start-up.

So give me some advice, please. How to write fastest php code and whats the best way to do it? The most important thing I want to hear about is the caching of the compiled code.

Thanks!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Confusion about PHP performance

Post by Christopher »

Changed the title because it looked like "ColdFusion and PHP performance".

If you are looking for performance improvements via caching then you can both cache in you own code and use accelerators like APC, Zend, etc. that add caching to the server.

And finally ... although from what you wrote I doubt that it would make any difference ... do you actually have real performance problem that you are trying to solve? My guess is that this is personality driven Premature Optimization. If you are truly struggling mightily with real performance problem then you should probably not be using PHP at all or at the least coding the performance critical sections of your application in C as a PHP extension (or you could just get more and bigger servers).
(#10850)
bugrush
Forum Newbie
Posts: 15
Joined: Tue Dec 16, 2008 3:00 pm

Re: Confusion about PHP performance

Post by bugrush »

Thanks!

Premature optimization it is, I know. I haven't had any performance problems but parsing like 2000 lines of code for every request that uses only a small part of it is a waste of resources. I'll check out APC. I don't really care about small websites but right now I'm working on a bigger project. PHP surely isn't the right language to do it but they want it aweb-based. I don't think it will have any performance problems even if it's written in PHP. I'll think about writing it in C#.

When I tried Drupal and Mambo on localhost, I got 1-3 sec lag on every click (3.2 ghz pentium d). Drupal had very serious installer :D This is stupid.

Ok, so what do you think about includes in PHP? Including every piece of the code every time isn't good idea, is it? How much should I use include switches to eliminate unnecessary code? I know, this can't be answered :p but maybe you have some examples for me?
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Re: Confusion about PHP performance

Post by matthijs »

From what you describe, you basically use basic procedural scripts. PHP is very fast, certainly with those.

Just take a look at what happens in a few OOP frameworks, how many files are included, how many thousands of lines of code are run, how many classes are instantiated, etc. What you do in your scripts seems like peanuts. 1-5 includes is nothing. Have you actually measured the performance of your scripts? My guess is that it runs in milliseconds. Doesn't make much sense to worry about performance in that case.

With performance, don't forget that most often the most can be gained from what is happening at the client-side (html, css, http requests, javascript, etc). For many websites, 80% of the response time is spend at the front end. Yahoo has a lot of good info on the subject online.

So first test the performance and only start worrying if you measure a real problem. And if you do measure a problem, look at the solutions which are the most effective first (maybe use a CSS sprite for the images on the page for exaple)
bugrush
Forum Newbie
Posts: 15
Joined: Tue Dec 16, 2008 3:00 pm

Re: Confusion about PHP performance

Post by bugrush »

Thank you matthijs!

I have measured the performance of my scripts of course and it runs in milliseconds as you guessed. I started programming in C++ and because of that I feel that even those results in milliseconds are still mocking my processor capabilities.

I worry because this could change my PHP coding style a lot. I don't want to rewrite whole programs if I happen to run into performance problems. Problems caused by output and connection speeds are much easier to solve. I just found out that PHP has autoload function which is called when trying to create an object of an undefined class. I may even start using OO programming in PHP.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Confusion about PHP performance

Post by Eran »

If you writing object oriented code, you could use autoload to load your classes' files on demand. This was you include only what your script actually needs.
User avatar
VirtuosiMedia
Forum Contributor
Posts: 133
Joined: Thu Jun 12, 2008 6:16 pm

Re: Confusion about PHP performance

Post by VirtuosiMedia »

I'm definitely not an expert, but I think you may benefit from switching to an OOP style, though it will probably take some adjustment in the meantime. As was mentioned before by matthijs, the bulk of your load time is going to be caused by client-side issues. This is the resource he was referring to at Yahoo. It's more than worth a thorough read. Another thing to consider that may affect load times is the number of queries you take to build each page. If you can reduce that in any way, it may help with both actual and perceived load time.
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: Confusion about PHP performance

Post by kaisellgren »

Does your project use a database? If yes, you may want to cache certain less or more static data and avoid useless repetitious database access. You may also cache some complex PHP processing. Caching is something that gives lots of performance. Also use some PHP accelerator.

http://en.wikipedia.org/wiki/PHP_accelerator

Moving to OOP might be a good idea, because in your case - including a few files only - it would not have a big impact on using classes. This way you would also optimize and improve your project easily over time. When you notice a bottleneck, you just improve one class or so.
Post Reply