Getting the most out of PHP doesn't take much but like every thing in life practice makes perfect.
What are the bottlenecks when it comes to programming?
network
hard drive
memory/bus
cpu
... from worse to best. How do these apply to a PHP script? Well first you need to understand how PHP works. PHP is an interpreted language. PHP only delivers one page at a time. You really need to understand those last two statements to design PHP applications that perform well. It's much different from designing compiled applications where all the piece work together. PHP only see one page at a time, everything that doesn't contribute to producing that page is reducing performance.
1. Server gets a file requests. ( network )
2. Server passes request to PHP. ( memory, cpu )
3. PHP reads script. ( hard drive )
4. PHP converts script to bytecode. ( memory, cpu )
5. PHP reads any included files, repeat 3-5 as needed. ( hard drive )
6. Script may accesses database. ( hard drive, memory, cpu and maybe network if on separate server )
7. Script may accesses remote data. ( network )
8. PHP/Server send page as response. ( network )
What are some of the specifics things that can increase performance?
Reduce the number of files included. For example if your using templates combine those templates into one file. They will eventually be combined anyway, correct? This doesn't mean you have to use just one template file just mean compiling them into one. You could even write a program to do the compile as necessary. Program for the page, not the application.
Reduce the size of those includes. Your production script does not need comments or white space. Run your scripts through PHP using the "-w" flag this will strip the comments and white space. If your including 30 "common" function on each page but only using one or two on each page. You need to do something about that, because they're not common to each page.
Reduce the size of database queries. Database queries can be even worse than included files. Especially poorly written queries. Every time you use one of theses "*" in a query, ask yourself if your going to use all that data? If the answer is no, then insert only the field you'll need. There no excuse for laziness. Use LIKE (and regular expressions) sparingly in database queries. If your using LIKE to perform searches this isn't a good idea. The database will INDEX everything. Write a search program with the appropriate database structures and index only what is needed.
Reduce the number of database queries. Basically, you need to decide what is static and what is dynamic. If the title and article on page are not going to change then they don't need to be queried every time the page renders. However the user comments, may change and should be queried every time the page renders. This maybe hard to do but it could have the biggest benefits to scalibility. When site grows to need more than one server your database may no longer be local. When that happens, your now faced with a new bottleneck - the network - every query better be absolutely necessary.
Programming is more than just writing code, it's a process. No matter what methodology you follow the process is always the same. Design, develop and maintain. Don't skip the design stage just because you want to start coding. Some of the biggest mistakes are made in the design stage. Design mistake can't be fixed in the maintenance stage.
With PHP, KISS ( Keep It Simple Silly ) is mandatory.
What are the bottlenecks when it comes to programming?
network
hard drive
memory/bus
cpu
... from worse to best. How do these apply to a PHP script? Well first you need to understand how PHP works. PHP is an interpreted language. PHP only delivers one page at a time. You really need to understand those last two statements to design PHP applications that perform well. It's much different from designing compiled applications where all the piece work together. PHP only see one page at a time, everything that doesn't contribute to producing that page is reducing performance.
1. Server gets a file requests. ( network )
2. Server passes request to PHP. ( memory, cpu )
3. PHP reads script. ( hard drive )
4. PHP converts script to bytecode. ( memory, cpu )
5. PHP reads any included files, repeat 3-5 as needed. ( hard drive )
6. Script may accesses database. ( hard drive, memory, cpu and maybe network if on separate server )
7. Script may accesses remote data. ( network )
8. PHP/Server send page as response. ( network )
What are some of the specifics things that can increase performance?
Reduce the number of files included. For example if your using templates combine those templates into one file. They will eventually be combined anyway, correct? This doesn't mean you have to use just one template file just mean compiling them into one. You could even write a program to do the compile as necessary. Program for the page, not the application.
Reduce the size of those includes. Your production script does not need comments or white space. Run your scripts through PHP using the "-w" flag this will strip the comments and white space. If your including 30 "common" function on each page but only using one or two on each page. You need to do something about that, because they're not common to each page.
Reduce the size of database queries. Database queries can be even worse than included files. Especially poorly written queries. Every time you use one of theses "*" in a query, ask yourself if your going to use all that data? If the answer is no, then insert only the field you'll need. There no excuse for laziness. Use LIKE (and regular expressions) sparingly in database queries. If your using LIKE to perform searches this isn't a good idea. The database will INDEX everything. Write a search program with the appropriate database structures and index only what is needed.
Reduce the number of database queries. Basically, you need to decide what is static and what is dynamic. If the title and article on page are not going to change then they don't need to be queried every time the page renders. However the user comments, may change and should be queried every time the page renders. This maybe hard to do but it could have the biggest benefits to scalibility. When site grows to need more than one server your database may no longer be local. When that happens, your now faced with a new bottleneck - the network - every query better be absolutely necessary.
Programming is more than just writing code, it's a process. No matter what methodology you follow the process is always the same. Design, develop and maintain. Don't skip the design stage just because you want to start coding. Some of the biggest mistakes are made in the design stage. Design mistake can't be fixed in the maintenance stage.
With PHP, KISS ( Keep It Simple Silly ) is mandatory.
Comment