- PeterMoulding.com
- Author
- Trainer
- Speaker
- Business Coach
- How to write a How To book
- PHP Courses
- Speaking
- Web Architect
- Australia
- Books
- Authors
- Akkana Peck
- Alex Berenson
- Andrew Nugent
- Ben Sanders
- Brock Clarke
- Chris Simms
- David Mercer
- Dianna Mullet
- Don Winslow
- Dori Smith
- Harlan Coben
- Jack McDevitt
- James Wines
- Jerry Yudelson
- John Grisham
- Kevin Mullet
- L. E. Modesitt Jr.
- Laurell K. Hamilton
- Marshall Karp
- Martina Cole
- Michael Marshall Smith
- Michel Roux Jr
- Nadia Sawalha
- Philip Pullman
- Raymond Khoury
- Richard North Patterson
- Robert Masello
- Sally Roth
- Sarah Langan
- Stella Rimington
- Stephen Booth
- Stephen King
- Stephen Leather
- T.C. Boyle
- Tom Negrino
- Tony Hillerman
- Urban Waite
- Val McDermid
- Valerio Massimo Manfredi
- Beginning GIMP
- Beginning Visual C++
- Culturalism
- Fiction
- A Drink Before The War
- A Talent for War
- Bag of Bones
- Blood and Ice
- Burn
- Dark Lady
- Dead Line
- Eclipse
- Empress of Eternity
- Exley
- Flipping Out
- Just One Look
- Nightfall
- Pet Sematary
- Savage Moon
- Skinwalkers
- Starvation Lake
- The Fallen
- The Gardens of the Dead
- The Jump
- The Last Templar
- The Mermaids Singing
- The Midnight Mayor
- The Secret Soldier
- The Summons
- The Terror of Living
- The Testament
- The Tower
- Under the Dome
- Virus
- AJAX and PHP
- Aging with Grace
- Food books
- Green Architecture
- Life Is So Good
- SQL: The Complete Reference
- The Backyard Bird Lover's Ultimate How-to Guide
- The Garden Gurus
- Authors
- Sustainability
- -18 hours left to decide the future of Australia
- Campbells vegetable stock or Massel vegetable stock?
- Carbon Sequestration
- Carbon tax for Australia is a fraud
- Copenhagen will fail
- Cost of living in Australia
- Dick Smith jumps on the population bandwagon
- Dry Run: Preventing the Next Urban Water Crisis
- Energy Saving Lights
- Garlic
- How many people can live in Australia?
- Its obsolete, throw it out!
- Julia Gillard offers 9.9 billion dollars bribe to Rob Oakeshott
- Laundry detergent
- Petrol or Diesel?
- Reflective foil batts kill
- RoHS
- Sea level to rise 3mm due to climate change
- Solar power
- Spring again in Sydney
- Sustainable fuels
- The CRUD Tax is back
- The people who make building regulations do not own houses
- Water efficiency
- Which insulation is safer, foil or wool?
- Will Australia reduce greenhouse gas emissions?
- Technology
- Android or Blackberry or iPhone or a flip phone?
- Apple versus Google 2011
- Cameras
- Cars
- Colour
- Burgundy
- Colour Blindness
- Colour Names
- Dulux colours
- Pantone colours
- Safe Colours
- Seculine ProDisk Mini colour balance card
- What Causes Colour Blindness?
- Hardware
- Batteries for the Digital Age
- Cables
- Cases
- Computer reliability
- Computrace
- Disks
- Astone ISO Gear 481E
- Best SSD for your notebook computer
- Disk block size
- Hitachi disk HDS722020ALA330
- LaCie USB 2.0 250 GB mobile hard drive design by F.A. Porsche
- SMART disk
- Samsung 2 TB HD204UI quiet low power disk for mass storage
- Seagate and Samsung merge disk business
- Select the right disk for your RAID array
- USB disk speed
- Western Digital WD20EARX 2 GB SATA 3 disk
- How long should computer hardware last?
- Keyboards
- Mainframe
- Memory cards
- Monitors
- Netbooks, notebooks, tablets, and xPads
- Network Attached Storage
- OLED Displays
- PC's are a thing of the past
- Printers
- Quiet
- Samsung Galaxy S
- Speed
- Television
- Tools
- USB
- Worst computer movies
- Xserve is dead. What next?
- Your backup will not work
- Z68 motherboards
- iPad or Acer Aspire One?
- IQ
- LG Intello Washing Machine
- Lack of a challenge
- Networks
- 802.11n wireless networking
- D-Link DIR-655 wireless router
- D-Link DWA-160 Xtreme N dual band USB adapter
- D-Link DWA-556 Xtreme N PCI Express desktop adapter
- MIMO
- NBN spends another $12 billion of our tax money on nothing
- National Broadband Network
- Netgear wireless modem router DGND3300 with 300 Mbps 802.11n
- Refrigerator kills wireless broadband
- Small Wireless Network
- TP-LINK TL-SG10005D 5 port gigabit switch
- TP-Link TL-WR1043N wireless N gigabit router
- Telstra Pre-paid Mobile Wi-Fi
- Where are the router plus proxy server combinations?
- Open Source documentation
- Software
- 7-zip
- Accounting
- Asterisk
- Audacity
- Backup software
- Bloat only in Windows
- CAD
- CDex
- Disk imaging software for copying and backup
- Exact Audio Copy
- Filezilla
- Firefox
- Java
- LibreOffice or OpenOffice?
- Linux
- 1 in 5 servers will ship with Linux
- Android phones outsell iPhone
- Another Move to Linux
- CentOS 5.5 installation on SSD and RAID 5
- Debian
- Debian 5.0.5 AMD64 installation
- Debian 5.06 installation
- Fedora
- Fedora or Ubuntu?
- Gnome or KDE?
- K9copy
- Linux 2.6.38
- Linux Gnome login settings lost
- Linux Mint
- Linux RAID, a rant
- Linux Speed
- Linux Time
- Linux reliability as demonstrated by Ubuntu 10.10
- Linux reliability as demonstrated by Ubuntu 11.4
- Linux still a struggle in 2011
- Linux workstation disk RAID 1
- Linux, NT, Windows, and SETI
- Linux, three years of progress
- London Stock Exchange switches to Linux
- Mandrake Linux 9.2
- The partition is misaligned by 48128 bytes - warning from Linux RAID
- Ubuntu
- How to fix the scroll bars in Ubuntu 11.4 Gnome
- Kubuntu 10.10 alternate installation on desktop with RAID 1
- POWbuntu
- Ubuntu 10.10 after 6 months use
- Ubuntu 10.10 alternate installation
- Ubuntu 10.10 desktop RAID 1
- Ubuntu 10.10 desktop RAID 5
- Ubuntu 10.10 desktop install on a netbook
- Ubuntu 10.10 desktop installation
- Ubuntu 10.10 netbook install on a netbook
- Ubuntu 10.10 server AMD64
- Ubuntu 10.10 upgrade to version 11.4 beta 2
- Ubuntu 10.4
- Ubuntu 11.10
- Ubuntu 11.10 first upgrade
- Ubuntu 11.4 after one month use
- Ubuntu 12.04 beta1 desktop amd64
- Ubuntu One
- Ubuntu by Microsoft?
- Ubuntu desktop upgrade 10.4 to 10.10 failed because I did not check the media
- Ubuntu strikes again
- Upgrade Ubuntu to Linux Mint 12 LDXE for extra speed
- Yes, use Linux but not that distribution!
- Nero
- OpenOffice
- OpenOffice is now Apache Office
- Project management
- Scribus
- Software for Windows and Linux
- Text editors
- Time
- Todo applications
- Tomboy notes
- Top text editors
- Version control
- VideoLAN VLC media player
- Visio
- Webmin
- Webmin installation on CentOS for Web development
- Webmin installation on Ubuntu
- What is the most popular open source software today?
- Windows
- Another Windows person goes Linux
- BAD_POOL_CALLER
- Cygwin
- Microsoft Malicious Software Removal Tool cannot find a common virus
- One of the developers of Windows XP is criminally insane
- There are unused icons on your desktop
- W32time
- Which Windows version?
- Windows 7 Home Premium
- Windows XP Stop 0x0000007B during installation
- Windows XP is a disaster
- Windows processes
- XML
- Zip, bzip, gzip, or 7zip?
- configFree
- Technology Succession Planning
- VoIP
- Web Sites
- Drupal
- Do Drupal themes have to use the GPL?
- Drupal 7
- A better search facility for Drupal
- Drupal - performance or flexibility
- Drupal 7 Fields are hard to fix
- Drupal 7 new features
- Drupal 7 ships on January 5
- Drupal 7.14
- Drupal 7.4 hits PeterMoulding.com
- Drupal function sequence
- The evolution of a module
- Undefined index: headers in DefaultMailSystem->mail() (line 54 of /modules/system/system.mail.inc).
- Undefined index: to in DefaultMailSystem->mail() (line 83 of /modules/system/system.mail.inc).
- implode(): Invalid arguments passed in DefaultMailSystem->format() (line 23 of /modules/system/system.mail.inc).
- Drupal 8
- Drupal Code Load Cut
- Drupal How To
- Drupal Modules
- Backup and Migrate
- Browscap
- CKEditor with Drupal WYSIWYG
- Captcha
- Cel
- Colorbox
- Content Construction Kit
- Content type
- Devel module for Drupal
- Drupal Rules as an automation language
- Drupal Spam add-on module
- Form alter to node
- IMCE
- IMCE Wysiwyg bridge
- ImageAPI
- Jdog
- Lightbox2
- Module variable
- Node Gallery Access
- Node_Gallery
- Path
- Path redirect
- Pathauto
- Pet
- Search
- Service links
- Session Variable
- Statistics
- Taxonomy
- Token
- Token ex
- Transliteration
- Trigger
- Watch
- Other modules
- Drupal Training
- Drupal access controls need a major rewrite
- Drupal coding tricks
- Drupal performance
- Drupal themes for the future
- Drupal.org colours
- Import existing data into Drupal
- Multiple Web sites made easy using Drupal multisite and the right start
- drupal_lookup_path()
- Adobe PDF
- Apache
- Apache Mahout
- Audi.com
- Bleet
- CSS Strikes Again
- CSS or xCSS
- Can you believe Facebook or email?
- Content Management Systems
- Databases
- Facebook scam
- Font
- Fonts
- HTML
- Install Apache, MySQL, and PHP 5 in Ubuntu 11.4 using the Ubuntu Software Centre
- Language Codes
- Marketing
- Memcache
- Nginx
- Open source development hits another roadblock
- Oscars
- PHP
- SPDY
- Search software
- Techoni.com.au
- Theme themes
- Things to hate on Web sites
- U.S. Patent No. 6,985,875
- Virtual Private Server
- Visible Improvement
- Web 4.0
- Web browser usage
- Web browsers
- Web site development
- Bluefish
- Crying over spilt code
- Eclipse and PHP
- Getting a Git client, a story of ancient technology and pain
- HTTrack
- MVC
- Netbeans
- PHP or ..., CakePHP/Symfony/ZF versus ...
- Programming
- Superfish
- Web browser emulators for testing your Web site
- Web development frameworks
- Web site books
- Web site development on your own computer
- Webmin or phpMyAdmin or cPanel for creating databases?
- aiki framework
- jQuery
- Views development - Learn Fields first
- Views development - Learn Actions and Rules
- jQuery .each()
- jQuery .has()
- jQuery .is()
- jQuery and Firefox Firebug
- jQuery children
- jQuery for people not using Drupal - Installation and getting started
- jQuery hover
- jQuery hover de-duplication example
- jQuery or CSS?
- jQuery performance
- jQuery tests
- Web site hosting
- Westpac Web site still broken after two years and ten months
- Wordpress wins another CMS survey
- Drupal
PHP performance
Submitted by Peter on Wed, 2010-12-01 12:11
technology:
PHP performance questions pop up every time a Web site jumps from 2 page reads per day to 2000 page reads. Most of the performance limitations in a Web site are nothing to to with PHP but there are a few small things to check before laying the blame elsewhere.
Lack of memory
Lack of memory is the most common Web site performance issue. PHP lets you make great use of memory and gives you more flexibility than some of the competing technologies. You get to choose between reducing memory usage and speed.
Limited memory, limited performance
You can save a little bit on memory and put up with limited performance. Some of the competing technologies will not let you do this. They require a minimum amount of memory and break with anything less. PHP works with a wide range of memory, gives you installation choices to reduce memory usage and gives you programming choices to reduce memory usage.
I could go on and on about the special things PHP does to minimise memory usage. Your main decision points are the installation step, where you switch off any optional modules you are not using, and the programming spots where you bring a lot of data into memory. I will give you one example of limiting data in memory.
You want to list a lot of items in a page and print a variety of summaries in different sequences. The fastest way to process the data is to read it into an array in the sequence you want to print then loop through the array summarising the data into other arrays then sort the summaries arrays then print everything. If you want to save memory in your PHP program, you can skip the array and bring the data into memory pre-sorted and pre-summarised. Your PHP program will use less memory and take longer because it will perform several database requests instead of one. You choose to minimise memory or to get maximum performance.
Lots of memory, lots of performance
I questioned the performance of one Web hosting VPS advertised as guaranteeing 1 GB of memory. The engineer found it was set to 0.3 GB because that was what they used to do a long time ago for that type of account. This was an old VPS account that was switched from 0.3 GB to 1 GB months before then mysteriously slipped back. The difference between 1 GB and 0.3 GB was impressive. Using only 0.3 GB, Web pages frequently timed out then the system had to log the errors, using up more of the limited memory.
I could have spent weeks reinstalling, reconfiguring, and reprogramming all the software to use less memory. Looking at the cost of VPS accounts with the two memory sizes, the extra memory costs less than $10 per month. My cost to reduce the memory usage to fit 0.3 GB would be between $5000 and $20000, requiring somewhere between 41 years and 167 years to save enough money to cover the expense.
When you have plenty of memory, you use some for a PHP opcode cache. This is special software that cuts out some of the repetitive work in PHP. The feature is so useful that the next version of PHP, version 6, will arrive with the APC PHP opcode cache built in. For PHP 5, you have to switch on a PHP opcode cache yourself.
Database
Databases arrive with different ranges of settings. The most common database, MySQL, has settings files configured for performance or limited memory usage. You can read about the options at the MySQL site. MySQL tries to save time by saving indexes and query results in memory for reuse. You do save time by giving MySQL enough memory to save frequently requested data. If you give MySQL too much memory, Apache, PHP, Linux, and everything else starts to slow down because they are all short of the memory they need to perform quickly. As a general rule, you want to tune all the major components together to share memory equally.
The next consideration is the low cost of memory. An extra gigabyte might cost only $20 per month. Tuning all the applications might consume thousands of dollars of your time and not produce a significant result. If you use a VPS, ask your ISP to check the performance and settings. They will occasionally change something that will make a difference. They might move you to a less crowded server if the server is overloaded. Mostly they suggest you move up to the next level account, with more memory and processor speed, because that change is really easy and cures a lot of problems.
Apache alternatives
Your Web site runs under Apache because most of the world's Web sites run under Apache. There are alternatives to Apache. Some of the alternatives use less memory some of the time. Your ISP might recommend an alternative. Do not replace Apache yourself until you have a lot of experience and great performance measuring tools. You can run into problems and not improve performance. You can end up with a configuration that will not let you add some new feature you need.
If your ISP supports an alternative then jump in and depend on them to fix any incompatibilities or limitations. For everyone else, replacing Apache is number 11 performance improvement on the top 10 list of things you should work on. Replacing Apache produces the least impressive performance gains and the gains usually occur only on very large sites.
PHP opcode cache
This is the big improvement you can make for PHP. Every other change is generic and will work across a variety of technologies. An opcode cache is the only PHP specific change.
PHP scripts are compiled to an internal instruction set called opcode. This is the same as Java and a whole lot of other programming languages going back to the 1980s. Instead of recompiling the code for every Web page request, you can keep the opcode in memory and save the compile step. An opcode cache does that exactly that. There are several you can choose. One of the choices will be built into PHP 6.
You might remember stories about the disastrous performance of Java back when Sun controlled Java. Java was interpreted, making it the slowest programming language under the sun. Every line had to be reread and reinterpreted for every use. Nobody has used that approach since the early 1980s. PHP 3 from way back in 1997 already used the far better approach of compiling into memory. In 1997 Microsoft rewrote Java to compile into memory, just like PHP, then in 1999 IBM rewrote their version of Java to do the same, just like PHP, and finally Sun drag the Sun version of Java into the 1980s world of high speed program execution.
PHP was the speed leader for a while due to the compile into memory approach. By the time the biggest competitor, Java, caught up, PHP already had an opcode cache to make PHP faster. An opcode cache makes typical PHP based Web site code 10 to 20 times faster. A large content management system using an opcode cache is typically limited by the speed of the database. Without an opcode cache, the code might slow down to slightly slower than the database, making PHP look like the cause of all the delays.
Installing a PHP opcode cache is easy. Most ISPs provide PHP preconfigured because it saves them processor cycles. If you use a hosting account or a VPS with something like cPanel, there is a selection box you can tick to install APC or one of the other PHP opcode caches. Check your account management pages now. If you cannot find the setting, ask your hosting company.
measured speed improvements
50 times faster
A program to build an image from components ran 50 times faster with an opcode cash. The program was one I mentioned in a presentation about graphics to PHP International in Frankfurt. The program read an instruction file and assembled the image using the instructions in the file. The process used hundreds of objects and looped through 100000 commands from the command file. The image assembly program was used on several Web sites and the 50 times speed up occurred when the operating system had enough memory to cache all the component files in memory.
The same speed up occurs in pure PHP code applied to analysing logs and similar activities. The bulk of the overall processing time might be in the database, not the PHP code, and the speed up applies only to the PHP code.
20 times faster
PHP scripts producing Web pages perform 20 times faster. If all the CSS files, Javascript files, and images are cached in the Web browser plus you access the Web page locally, you see a 20 times improvement.
10 times faster
The PHP scripts producing Web pages 20 times faster, when viewed over broadband, arrive 10 times faster.
No difference
The PHP scripts producing Web pages 20 times faster, when viewed over dialup, arrive in the same time because the download is the biggest time consumer, not the server processing time. The same happens with content management systems when you switch on database intensive options, the database chews up all the time, not PHP. As a comparison, using a popular CMS, a regular page requires 10 to 20 database accesses but a page producing a fresh word cloud display can require 100000 or more database accesses. Some Web sites have their word cloud displays set to rebuild for every page instead of using a background cron task to rebuild each hour or day.
Green
An opcode cache is environmentally green because you use fewer CPU resources to deliver the same Web pages, reducing electricity use and green house gas emission. If you run your computer on solar power, you can still contribute because your power saving gives you more green electricity you can send to the grid for use by other people. What difference might it make? A 125 watt processor might power up to only 25 watts when delivering a web page. Your total computer room might save 300 watts because other components, including the air-conditioner, can wind down when your processor is not burning up the environment.
PHP opcode caches
APC
APC, the Alternative PHP Cache, is the one built into PHP 6. You might as well use it now if APC is an available choice in cPanel, Webmin, or whatever you use to manage your Web site. Some of the other choices have special features you might occasionally need for some Web sites. Read about APC at php.net/manual/en/book.apc.php.
eAccelerator
eAccelerator, from http://eaccelerator.net/, is the only option presented by some ISPs in cPanel and equivalents. You need the latest version of eAccelerator for the latest version of PHP and your ISP will usually provide the right version. using the default settings, I cannot find any performance differences between eAccelerator and APC.
NuSphere PhpExpress
NuSphere PhpExpress is similar to all the other PHP opcode caches and offers one extra feature if used with NuSphere NuCoder, the NuSphere PHP Encoder.
The encoder is a way to distribute the opcode encoded so that you do not distribute the source code. This encoding completes with an encoder from Zend. Your customers are more likely to use Zend products than NuSphere products with the NuSphere PhpExpress having one advantage, it is free. You could buy both nuSpere NuCoder and the Zend equivalent then send encrypted code to everyone.
Zend
Visit Zend, www.zend.com/products/server/, to see their range of products. Like NuSphere, Zend products are an advantage when used together. They opcode cache does nothing special by itself. When you develop with Zend products, you can encrypt your opcode with a Zend product then sell the encrypted code to people who use the Zend server. The Zend opcode cache does not currently provide significant performance improvements over the other cache products.
Xcache
Xcache is popular and was the flavour of the month a while back. Xcache is not offered by any of the hosting companies I use or my customers use. Again I recommend those without years of experience should choose whatever is supplied and supported by their Web host.
Windows Cache Extension for PHP
Right, Microsoft get into the act. This is an attempt to make PHP effective under Microsoft's IIS. Use Apache, not IIS. You can use Apache, PHP, MySQL, PostgreSQL, and a whole bunch of other stuff on Windows. Windows is not a problem when you use the Pro version with NTFS. IIS is the big problem. IIS has some really nice features but it was history a long time ago and those really nice features are popping up in the open source alternatives to IIS.
Windows with NTFS gets a lot of its performance advantages from smart caching of the file system, which means you need a lot of memory. You might survive with slightly less memory using Linux and Apache because both Linux and Apache install far fewer features by default.
Conclusion
Use a PHP opcode cache. Use whatever your Web host recommends. If you have a choice and do not need a special feature of another cache, use APC because that is the one you will use in PHP 6.








