___________    .__       .__      __        ____  __.                    .__             .___               __________                       
\_   _____/_ __|  |___  _|__| ____|/._____ |    |/ _| ____   ______  _  _|  |   ____   __| _/ ____   ____   \______   \_____    ______ ____  
 |    __)|  |  \  |\  \/ /  |/  _ \/  ___/ |      <  /    \ /  _ \ \/ \/ /  | _/ __ \ / __ | / ___\_/ __ \   |    |  _/\__  \  /  ___// __ \ 
 |     \ |  |  /  |_\   /|  (  <_> )___ \  |    |  \|   |  (  <_> )     /|  |_\  ___// /_/ |/ /_/  >  ___/   |    |   \ / __ \_\___ \|  ___/ 
 \___  / |____/|____/\_/ |__|\____/____  > |____|__ \___|  /\____/ \/\_/ |____/\___  >____ |\___  / \___  >  |______  /(____  /____  >\___  >
     \/                                \/          \/    \/                        \/     \/_____/      \/          \/      \/     \/     \/ 
                                                                                                                               finally @ home
Quick References for High Performance PHP Scripts (Information) 
---------------------------------------------------------------------
    by Fulvio Oliveira, 2007-02-22 11:03 

No matter the hardware, there are a number of things to consider when
coding a system where performance is critical.


1. die()
This is one of the most important functions.

Create a function to put the page together with the result from your
performance critial engine. This function should be responsible for
holding absolutely all the design and layout used in your website. Use
the least parameters as possible, parsing only the information you
really need into it.

If you need to log some information, this is also a very useful place
to call it. If you include this function right in the beginning of
every file, it can even record the processing time, so you can analise
your performance.

Then, code your PHP from the most common request to the least
expected, and NOT from the quickest to the longest. Everytime you get
enough information for the user, call this function. This coding style
has two main benefits:

a) you will not face the problem of having many if-then-else or switch
in the code;

b) you may completely separate the layout and design from the
production code, allowing you to facelift the website, put a different
brand on it, or even change the engine completely.

file structure.inc.php:

$PAGE_REQUEST_TIME = microtime(true); function structure($content) { $processing_time = 1000 * (microtime(true) - $GLOBALS['PAGE_REQUEST_TIME']); die( << ...menus and stuff... {$content} < / body > < / html > EOT ); }

file index.php:

request 'structure.inc.php'; if ...most common command... { ... handling it... structure(response); } ...least common stuff... structure(response); ...only few people will want this... structure(response);

2. regular expressions I thought I had been doing them wrong when I tested, but after studying them for a long while, they are very slow. It is advisable to use other methods rather then regexp. If extremely necessary, preg* functions are a bit faster than ereg* functions. 3. arrays Abuse arrays. PHP and Linux can deal pretty well with the available memory. Arrays are much quicker than MySQL, no matter how simple and/or well indexed your table might be. Arrays in PHP can be nested, can use aphanumerical keys for easy access, can be sorted by their keys or values, and have tons of quick functions to deal with them. 4. MySQL As much as I love MySQL for storing data, it is much slower than arrays. Biggest queries are the worst. Fetching information is not really a good idea, but comparisons between multiple databases may get the situation much worse. Indexing and rearranging the data may get you much better execution times, and any string manipulation executes faster in PHP. If you are using MySQL to collect and intersect information from many different tables, consider putting this information in an array and intersecting the array itself. 5. strings PHP is awesome when it comes to string manipulation. There are many quick string manipulation functions. For most of the algorithms tested, they were much much much quicker than regexp. 6. regexp x similar_text If you are using regexp just to correct typos from you user for then plugging his info into MySQL, consider using similar_text. This function is pretty quick for most uses. 7. code on an old machine ...and forget about getting the same code to run 1 000 000 times to get any measurable difference. Especially here in Australia, where you can find old hardware on the bin. When you code something on an old machine, it is easy to *feel* any difference in execution times. Just ensure that you put this machine together yourself, so you know all the bottle necks. 8. kiss Kiss stands for Keep It Simple, Stupid. If it is not absolutely necessary, you would better not even think about coding it. 9. test Sadly, testing is the best way of knowing your code. Ensure that no other process bothers your test. If your script is too complicated, you may measure each process, but ensure to stop the watch while recording the logs (so the recording does not affect your script). Be creative, break rules. 10. Python x MySQL This is a tricky one, but, depending on the information you need from MySQL, it may be faster to retrieve them by a very carefully written Python script. The same Python script can run for days on the server, so it can retrieve MySQL information every 5, 10 or 240 minutes and store them in big dicts. PHP may connect to the Python script through a socket and get the information. 11. C How deep do you need to go? Assembly, maybe? These are only general considerations. Of course, every system is different. --------------------------------------------------------------------- Read more on Information
                                           .    .          .      ...................                  .                                                        
                                                   .  .  ..    .?ZZZOOOOOZ$$8DDNDDDD7..... .     ..                                                             
                                                    .   ....?I7$8OOOOOO8DDDD88DDNDDDDDNDZ,..                                                                    
                                        .           ...,O88DDDDD888DDDDNDNMNDD8D8OZ8NNDDDDDO....                                                                
                                       .    .   ...,?Z88NNNN8ZZO88DD8OODO7I$8DDDDDDDNNNDNDDD8Z~...  ..                                                          
                                            ..,+ZO8DDNDDNDDDDDNMNNDDNNDDDNNNNNNNNNNDNDDDNDNNDN88$?,..                                                           
                                        ...?OOZODNNNDNDDDNNNNNDDOOZZ8NDNNNNNNNNNNNNNNNNNNNNNDDDDD88O,..                                                         
                                      ..?$OODNNDDDN8ZODNDNDDDNNDDDDNDDDDOOODDNNNNNNNNNNNNDDNDDDN888D8O?.                                                        
                           .         .=$ODD88DNNDDDDNNNNNNNNNNDDDDDDDDDDDNNDNNNNDDDD8O8DDNNDDDDD88D8ZODO..              .                                       
                                  ..I$8DDNNDMD8D88DDNDD8ZO8DNDD8NNDNNDNNNNNNMNNNDDNN88DDDNDDDDDDDDDDOOZZ$?..    .                                               
                                ..~ZDNDDNNNDOO8DDDNNDDDDDNNNDDNNNNNNNDDDDD8Z$ZODDDDDDDDDNNNNNNNNDNDD8D8O..?Z.... ..         ....                                
                               .=DN8$ONNDDDD88DDDD8DD88D88OZ8ND8ZZZZDDDDDDNNNNNNDDNNDDNNDDNNNNNNDDDDDD88$7$Z8$..                                                
                              ..Z8DDDNDDDDOODDNDDNND88DDDD8D88D8DNNNNDNNNNNNNND888DDNNNDNDNNNDDDDDNNDDD8OZZZZOD$..            ..                                
                            ..=888DNDDNND888NDD888O8NN88888OOO8NDD88NNDDDDDDD888DNNNDDDDDDDDNNNNDDDDD8DD8OOOOOO8O7,.  .                                         
                          ..~ODDDDNNNNDDDDDNN8DNDNNDDDDDDOOOO8D8ND8888NNDDDDDDNNDNNNNDDDNDNNNNNNDDDDDDDD8OOOO888D8OZ..                                          
                         ..+NNDDDDNNDND8DDDNNDNNDNDDNNNDO8DOZOO8OO8DNDDDDDDDNDDD888DD8DDDDDDDDDDDDDDDDDDD8OOOOO8D8DNZ,.       ..                                
                     .  ..$ODDDNDNNND8DDDDDDNNDDDDDDNNNDDD88DNDDDD88DNDDDDDDNDDNNNN8ZZOODDDDDDD8DDDDDDD888OO88OO8DDDD$=. ..                                     
                        .$DDDNDDNDDDNNDDDNNDNNNNNNNNDDNN88OZDNDOODNNDDD88DD8888O88O$Z8DDDDD8D8888DDDDDD8OO88O8D8O8DD88?.                                        
                .     ...8DDDNNNNNNDDDNNDNDDDDNNNNNDDDNND8DNDDDDDD88DDDDD88DDDDDD888DD88O88888O$ZOO8888OZZOOOZZ8D8888OI.                                        
                       .$DDDDDNNNDDDDDNNNDDDDDNNDDNNNNNNDNNN8888DDDND8DDDDDDDDDDD88DD88O8O8888Z777$Z8888ZZOODOOO8D8888:.    ..                                  
                .      .DNDDNNNNDNDDNNDNDDDDD8O888DDDDD88DDDDDDDD88OO88O88888OOOOOODZZZZZ7$$7I+==+++7$ZOOZO88OZODDD888..                                        
                .      .NNNNDDDDNDDDDNNNNNNNNNNNNNDNNDNND88888DDNDDDDDDDDD88ZZOO8OO$II???77?=~=~~======+7$$ZZ88O88D88D8? ..                                     
                      .7NDDNDDDD8DDNNNNNNNNNNNNNND8888888888DDDNDDDDDDD8888OO8OZ$7?+++++?I+~==~:~~~~===+?I7$ZZZ8DDDDDDD8.                                       
              ..   . ..ZNDDNNDDDDDNNDDDNNNNNDNNNNNNNDDD88OO8DDDD8888Z$888OOO8Z777I++~~~===~:~:~~~~~~====++?I$ZZZ8888D8D8+..                                     
                     ..$NDDDDDDDNDNNNNNNNNNNDDDNNNNDDDDD8888OOZ$O88Z$ZOZ$$ZZ7I??:,,~::~====~::~~~~~~~===+++?I7ZZ88ZO8DD87.    ..                                
                     ..$NNNND88DNNNNNNNNNNNNNNNNDDDDDDDD8D8888DN888O8O8OZZ$7I+~:,,:~~~~~~:::::~~::~~~~==+++?I7$ZZZ8O888D+.                                      
                 ..  ..$DNDDD8DDDNNNNNNNNNNNDDDDNNNNNNNDDNNNDDOZ$77ZZZZ$7I+~,..,:,,:~~~~,,:::~~~:::~~~===++++I7$OZZO888D:.  ..                                  
                    ..?8NNNDNNNNNNNNNNNNNNNNNNNDDDD88888O88OOO887$ZZ$I??=~,,::::,:::,:,.,:,,:::::::~~~=====+??I7ZOZZO888=..                                     
              ..     .IDNDNNDDDDNNNNNNNNDDNNDD88DDDDOO$II7?=II?+$$$$I++=::....,,,...,,,,,,,,:::::::~::~~====+?I7$ZZZ888D:.                                      
                .  . .IDDNNNDDDDNNNNNDNNNNNDDDDDD88Z$ZO8OZZZ7$$Z$7I?=~:,:,......,,,,,,,,.,,,,:::,,:::~~~====??I77$ZOO8OD,. .                                    
                   . .$8DNNDDNNDNNNNNNNDDNDDDDDD88Z$?I77??7$IIII+=~:,,,,:......,,,,,,,.,,,,,,,::::::~~=~====++I7I7$Z8OOO:.    ..                                
                 ..  .$DNNNDDNNDDDDDDDNDDDDD88D888Z7??+?===+I+~:::~=:,::...........,,,,,,,,,,,:::::::~~~~~==+??II7$Z8888:..                                     
              ..   ..?Z8NND8NNNNDNNDDDDDDDDD888D8OZ$II?+~=~=~::~::~:,,,,........,,,,,,,,,,,,,::,::::::~~~~~=????7$$Z8ZOO=..                                     
                    .?O8DDDDNDDNNNNNDDDDDDDDDDDD8O$7I?+=~===::::::::,,,,,...,,..,,,,,,,,,,,,,,,,::::::~~====+???I77Z8OOO,.                                      
            ..     ..?ODDDDNDDNNNNDDDDDDNNND8888OZ$$?+=~~~==~:::::::::,,,.....,,,,,,,,,,.,,,,,,,,:::::~~===+++??I$ZZ88OZ.. .                                    
                   .,:ZD8DNNDDDDNNDDDDDDDDDD8888OZ$$?++~~=~~~:::,,,,,,,............,..,,..,,,,,,,:::::~~~===++??I$OOOOZI..                                      
              ..     .+88DDDDDDDDNDDDDNNNNND88DO8Z$7?+==~~~~::,,,,,,,,,............,,,....,,.,,,,:::::~~~===+++?IZ8ZOOO+.                                       
                     .,ODNDDDDD8NNDDNNNNNNNDOZOO$I??++===~~::,,,....,,..............,,..,,,:,,,,,::::::~======+?IZ8OO8O:.                                       
                     .~8DDDDNND88DDDNNNNNNNDOO8O7III?+==~::::,,,...,,,:,,,::,,::,,,,,,,,...,,,,::::::~~~=======+IO88O8Z:                                        
                     .ID8DDDNND8DDDDDDNNNNNDO88$7III?++=~::,,:::,::~~~:::::~:::~:,::,,,,...,::::::::~~~~=======+IO8OOOZ:                                        
                     .IO8DDDDD88DNNNNNDDNNNDO8ZI7?7I??+=~~~=?I?+?I7$$$$77$$77???+~:,:,..,:::~~~~=+++??I?++++===+?ZDOO$+.                                        
                     ..$DDDDDDDDDDDDNNDDNNNDOO$IIII7++?+?77$$$Z$ZZZO88OOZO88OZ7?++=:,,..:::~==+?$$$ZZZ888Z$$77II=7D8O7,..  .                                    
            .... ..    :DDDDDDDD88DDNNNNNNDDOZ$7II7I7$$77$$777$$I++??III?++++???++=~,,..:~~=+?7ZOOOOZOOOOO88Z$$$?7OOZ~. .                                       
                     . .8DDDDD8888DDNNNNNNDDZ$77III7$$$7++=++???$DDN8ZZ7I++=I?+====~:,,.::~=I77I?+?788ZZ7I7$OOZZZZZOO:..      ..                                
                      .:DDDD8DDDDDDDNDNNDND8$IIIIIIII?=~=~=+?I$88D?ID$?.=+?~~+??=~==~:::::~+III?+IZ8MNDZ7OO$?I7$$$ZZ7..                       .                 
                 ..  ..ODNNDDDDDDDDDNNNNDDD87IIIII?+~~~~=+?7OO$77$ZZ$7?=+I7I++??=~===~~:~~~=7I+~.=Z87NDZ==$ZI+++I$OZ..     .  ..             .                  
                .    ..88ZZZNNNDNNNNNNDNDD8ZI???I?+=~===?III+===~:,:~=+++==~~=++======~~:::~I77?I???7$7$II7$O7?+?$O$.                         .                 
                     ..I+=:~$O8DDDNNNNDDD8O$???+??+======~=+==~:,..,:~=~=~~~?+==~~~=====:::~?II?+====~===+++I???+Z$.   . ..                  .                  
                      .,~,:==~?ODDDDDDND8OZ7?+++++=~~==~=+===~~~===~~::~~===:::~~=~~=~==:,::=+???+=?+=====+??++?+$:.  .. ..   ..              .                 
            ..         .:::+=:,=7ZOODDDD8O$I+?+=++=~~==~~::~~=~~~=======~::,:::~~~~~====~:::~+??+????+++++??+++?=I..                                            
                       .:,~?~:,,~?7Z8DDD8Z7?+++=+==~~~~~::~~==~=====~~~:,,,,::~~~~~=====~::,~=??=~=+???+======+?~:.                                             
                       .,~=?,..:+?+?O8D88Z7?+++====~~~:::~~~::::::::::,,,,::::~~~~~==~=~~:,,::=??+======+?++=+??:.                                              
                   .   .,=+=,,:~,:~:$88O8$I+?++++===:::~~~::::::::::::::,,,,,:::=====~~~~::,::~+?++==~~~======++:.                                              
                      ..:~?:::~:..,:+=?777I?++??==~~~~~~::::::::::,,,::,.....,:=======~~::,,,:~~+=~~~~~~~~~=~~==~.                                              
                       .::?::::,,,:~+~=III??+??+=~~~~~~:::,,,,::,:,,,:,::::::~=+==~==~~~::::,,:~++=~::::::::~~===,.                                             
                       .~:++,~~~...,=:=III?I???+=~~~~~::,,,.,,,,,,,,,:::::,,~++=~~~==~:::::::::~+??+~:::,:::~~~~==..                                            
                 ..     .:~+:~~+:...:~~?IIIII?++~~~=~:::,,,,,,,....,:::~::::=+=::~~~~::::,..,:::=+?+=~~:,,::::~~=~..                                            
                        ..,~=:,,~~~~+?=?III????+~~~=::::,,,,,,,.,:::~~~~::::~++=:~~=====:,,,,,:~+++?===~,,,:::~~=:.                           ..                
                         ..:=~,.~::~II+I?I?????+~~==~:::,,,,,,,,,:::~=~~:::::=++=~===+++~,::,:~+?+++===~:,,::::~=:.                           .                 
                         ...::::=~,,~~=???????++~===~~:::,,:,,,,::~=~~,::,:::~~=++=~~~=+=~~~=+++++?+=~~~~::,:::~~:.                                             
                           .~~::::,,,:=+II??+?++====~~~:::::,,,,~==~::::::~~~:~~~~~~~~~=++?????I???++====~~:::~~~.                                              
                            .~~,:::,,==:?I??+++======~~~:~:::,:~=~::~~~~::~~~::~~::~::::~==++???????++++==~:::~=:.                                              
                             ..~:,,,~=,.+I7I++++=====~~~:,::::~=~~~~~~~~~~:::~:::::,::::::~==++++++?++++===~~~~~:.                                              
                          .    .~:,,,,..?77I?+++==~~=~~~~~::~==~~~~~~:::::::,::,,,,,,,,:~~~~===+=++++++?+++=:~==..                                              
                            .. .~+=:,::=7777??++===~~~~~~:::=~::~:::~~::~~~~:~~::,,..,,::~~~++++++++++?++++=:~+:.                                               
                               .7O88O8D877$7I??+===~~~~~:::~::~:~~~~+=+?+???++====~=,:~~==+???II????+?+++===:=+,                                                
                               .ZO8NDD8O7I77II??====~~~=~:,::,::=+I7I?I+?=,,~=~:~=+~~=++++I?++++ZZ$7II?+~~~:~=:....                                             
                           .    $8DDDD8O7I777II?==+=~:~=~:,::,,,:=I$?=7=++,.~~..,~,...~= ..+,,+=77?7I=~~~~,,+:.                                                 
                         ..     ?ZDD8OOZ$?I7III?+++=~~~~=~:,::,,:,:=$$7?+I+.,=~..,....,...,~.~?+:=7$+==~,.,~=..                                                 
                                ..$8DOOO7?IIIII?+++==~~:=+~:,::,::::=++?77=::?77..:,+:,::?I?~~.+I7I?++=:.,~~..                                                  
                                   .7OO8$???IIII??++==~::==~,.,,,:::::~~=+++?::~.,~.,....,=~+7I7I?+?++~~:==,. .. ..                                             
                              ..     .7Z7?+?777II?++===~::~=:..,,,::::~~===~+?I?~~:.=?++?I$77II+???+++~.=~,.                                                    
                         ..      ..  .IIII++II7I??+++===~::~=:,,,,:::::~=====~~:::=~=~~====+++???++++~:~~...                                                    
                           .         .$?I?+=+IIIII???+===~~~+=:,,::::::::::~=~::....~:::=+==++??++++=:+=..    . .                                               
                                 ...=7ZI+I+==?I?II??+++++==~=?+:,,:::::::::::::~,...:::~~~~=+++?+++===:..                                                       
                                ...7ZZO7~I+==+???III+++++++=~=++,,::::::::::~:,:,,:::::~~~===++++====:..                                                        
                               .7$ZZ$$OZ.??===++????I?++++++=~=?:,,::~::::::::::,:::~~~~~~===++++===:..                                                         
                             .~$Z$$$ZZZO,:I+====++??????++===~=+=:,,::~~::::::::,,::~~~~=~==+++++==+..                                                          
                             .:$$$$$ZZZZ,.?+=====+??????++=====++~::::::::::::::,,,::~~~~===+++==+++.                                                           
                            ..,$$$$$$$Z$?.,?+=~~===+?????+==++==?+=~::::::::::::,,.,::::~===++==+?+,.    ..                                                     
                         ..  ..,$$$$$$$ZZ~.~=~~~~~==++????++++++??+=~::::::::::,...,,,,::~~=+++=?+:...                                                          
                           .....$$$Z$77$Z$:.~~~~~~~===+?++??+++++II?~:::::::::,,..,:,::::~====+++,..       ...                                                  
                             ..:ZZ$$I7$$$Z7,~~::~~~~~==+++??++++++?I?+~~:::::,,,::~=~,,,,:~==++?..  ..                                                          
                        .   ..I$ZZ$7II$$ZZZ7.:=:~~:::~==++++++????+????+~~~~~:::~~~=~::,::~~??=.    .                                                           
              ..  ..        .?$$$ZZ$777$ZZ$Z+..=:,::,:~~=====++????????I?+==~=======~~~~~==+II,.. .                                                             
                 ..       ..7$$77ZOZ7777Z$$$7I,.::::,:::~~======+?IIII???IIII???I??++?????77~=.                                                                 
                         ..+$777$OOO$777$Z$$Z$7~.::,,,.:~~~~~~~=~~~=?IIIIIIIIII777777II7?+??.,..    ..                                                          
                      ...:7Z$777$ZOOZ$77I$Z$$$77,.:~,,,,:~~::~~~~~~~:,:~==++++++===~=+?I+?I+,=,.                                                                
                   ....=$$ZZ77$7$ZOZZO$77I7Z$$$$7+..:,,,,:::::::::::::,,,,:::::::~~~+II?+?$+.?+.                                                                
               .....?$ZZ$ZZZ7777$$ZOZZZ$77II$Z77?I7:.:,.,,,,::~~::::::::,,,,:::,::~=?II+?7I::ZZ:                                                                
    ..    ......7$$$$$$Z$ZZZ77$$$$ZZZ$$Z7IIIZZ777III:..:.:,,,:~:::,,,,,,,,,,,::,::~+I7?+I7+.7Z$7.  . .                                                          
      ....:=7$ZZZZ$$$ZZ$Z7$Z$77$$$$ZZ$Z$O$IIIZZ77II?7I~.~:,,~:::::::,,::,,,,,,,,:~=?$I+=ZI,:$7$$~...                  ..    ..                                  
......,~I77777777777$$$$ZZ$$$77Z$$$$Z$$$ZZ7III$ZI777??II,.,:.,::,:::::,,,,,,:,,,=+?$7+=I?.=$$$7$$7+,....            ..                                          
I$$ZZ$Z$$$$$$$$$7III777$$$Z$$$$7Z$$$$$$$$Z$7II7$7I?II77?7+,.:,,,::::,,::::::,,:~?7$7++I?,?Z$77Z$$7777$7?,....    ..                                             
$$$$$$$ZZZZZZ$$$$ZZZ$$$7$$$$$$$$$$$Z$$$7$Z$7$7I7$$7II77IIII+,.,=::::,::::~~~~~+II7$???~:$$$$77$$$$$777$7777I:.....        .                                     
$$$$$$$$$$$$$$$$777$$$$ZZ$$77$$$$$$$$$7$77$$ZOO$+=~:==+?7??II+:.~=~::,:::~~~=?I?I7II:,+ZIZ$77$$Z$$$$$7777III77II+~,.......                                      
777$$$$$$$$$$$7$$$$$ZZZ$7OZZ$7777$$$7$$$ZZZZ,...?+=?=~,......:=+~.,~::::::~~++?7?:..I$$$$$7I$ZZ$Z$$ZZZ$$777II77III777I?+:........       .                       
77$$$$$$7777$$$ZZ$$$ZZ$$$ZZZ$7$$7$$$7$$$$ZZ+..~I??77I77I+=~,.......,=~====++??+=..:+O$$$7$$7$$$$$$$$$$$$77II77II7II??I777???~,,.......                          
                                                                                                                                                 GlassGiant.com