RE: [PHP] Freelance code optimizations
Jay Fitzgerald mailto:[EMAIL PROTECTED] on Saturday, August 09, 2003 10:05 PM said: I am a somewhat slow learner in that I cannot learn from reading - the stuff on functions, for example, is confusing to me - I can only learn by examples with explanations of why certain things are done. I have found that while loops* are faster than for loops**. . concatenation*** is faster than , concatenation, and both are faster than spaghetti code*. Post more questions if you need help with other things. Or just email me off list and I'll help you out (if I can). Also, to anyone else that has differing opinions on what I think is fast I'd like to send you the file I'm using to test this out. Consistently while loops are fast than for loops AND . concatenation is consistently faster than , and spaghetti. I'd be interested to hear what othe people have to say. == Jay Fitzgerald, Design Director ... Wow, your sig is WAY too long! HTH, chris. * while loop $ctr = 0; $val = 0; while($ctr10) { $val++; $ctr++; } ** for loop $val = 0; for($ctr=0;$ctr10;$ctr++) { $val++; } *** . concatenation $myString = Hello this is my string. . This is more of my string. . And even more of my string. . And even more of my string. . And even more of my string. . And even more of my string. . And even more of my string. . And even more of my string. . And even more of my string. . And even more of my string. . And even more of my string. . And even more of my string. . And even more of my string.; , concatenation $myString = Hello this is my string. , This is more of my string. , And even more of my string. , And even more of my string. , And even more of my string. , And even more of my string. , And even more of my string. , And even more of my string. , And even more of my string. , And even more of my string. , And even more of my string. , And even more of my string. , And even more of my string.; * spaghetti code Hello this is my string. ? echo This is more of my string.; ? And even more of my string. ? echo And even more of my string.; ? And even more of my string. ? echo And even more of my string.; ? And even more of my string. ? echo And even more of my string.; ? And even more of my string. ? echo And even more of my string.; ? And even more of my string. ? echo And even more of my string.; ? And even more of my string. ? echo And even more of my string.; ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Freelance code optimizations
I've never seen a comma concatenation operator in PHP and the following when cut and paste to a test PHP script generates a parse error under PHP 4.3.2. Could you elaborate on this operator? Is it part of the PHP 5 engine? Or are you mistaken in assuming the following works when perhaps you use this style as part of an echo statement normally. Cheers, Rob. On Mon, 2003-08-11 at 13:29, Chris W. Parker wrote: , concatenation $myString = Hello this is my string. , This is more of my string. , And even more of my string. , And even more of my string. , And even more of my string. , And even more of my string. , And even more of my string. , And even more of my string. , And even more of my string. , And even more of my string. , And even more of my string. , And even more of my string. , And even more of my string.; -- .-. | Worlds of Carnage - http://www.wocmud.org | :-: | Come visit a world of myth and legend where | | fantastical creatures come to life and the | | stuff of nightmares grasp for your soul.| `-' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Freelance code optimizations
Comparison to a variable is expectedly slower since the engine must perform a lookup into the running script's variable list. Since a constant doesn't require a lookup it's access time is O( 1 ) whereas the lookup of a variable will lie someplace between O( 1 ) and O( lg n ) depending on the algorithm used. Cheers, Rob. On Mon, 2003-08-11 at 15:44, Ray wrote: also, if you can compare to a constant, there is a notable difference as well. ?php $i = 0; $a = 1000; while( $i++ $a ){} ? real 0m10.268s user 0m10.000s sys 0m0.030s ?php $i = 0; $a = 1000; while( $i++ 1000 ){} ? real 0m7.057s user 0m6.880s sys 0m0.020s so if $a is something like the size of an array or another thing that affects the number of times the loop needs to be run. then if you can, reverse the order of the loop so you can compare to 0 (or another constant) ?php $a = 1000; $i = $a; while( $i-- 0 ){} ? real 0m7.111s user 0m6.870s sys 0m0.040s YMMV -- .-. | Worlds of Carnage - http://www.wocmud.org | :-: | Come visit a world of myth and legend where | | fantastical creatures come to life and the | | stuff of nightmares grasp for your soul.| `-' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Freelance code optimizations
On Mon, 2003-08-11 at 13:29, Chris W. Parker wrote: * while loop $ctr = 0; $val = 0; while($ctr10) { $val++; $ctr++; } ** for loop $val = 0; for($ctr=0;$ctr10;$ctr++) { $val++; } I get the following results (very consistently +/- .1) which agrees with your results and also shows an improvement over your own optimization. for( $i = 0; $i 1000; $i++ ){} time php foo.php Content-type: text/html X-Powered-By: PHP/4.3.2 4.42user 0.03system 0:04.60elapsed 96%CPU (0avgtext+0avgdata 0maxresident)k 0inputs+0outputs (528major+216minor)pagefaults 0swaps --- $i = 0; while( $i 1000 ){ $i++; } time php fee.php Content-type: text/html X-Powered-By: PHP/4.3.2 4.22user 0.01system 0:04.43elapsed 95%CPU (0avgtext+0avgdata 0maxresident)k 0inputs+0outputs (527major+216minor)pagefaults 0swaps --- $i = 0; while( $i++ 1000 ){} time php foo.php Content-type: text/html X-Powered-By: PHP/4.3.2 2.97user 0.00system 0:03.09elapsed 96%CPU (0avgtext+0avgdata 0maxresident)k 0inputs+0outputs (526major+216minor)pagefaults 0swaps --- Notice the large difference when the incrementation occurs within the while check itself. Cheers, Rob. -- .-. | Worlds of Carnage - http://www.wocmud.org | :-: | Come visit a world of myth and legend where | | fantastical creatures come to life and the | | stuff of nightmares grasp for your soul.| `-' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Freelance code optimizations
Robert Cummings mailto:[EMAIL PROTECTED] on Monday, August 11, 2003 10:55 AM said: Or are you mistaken in assuming the following works when perhaps you use this style as part of an echo statement normally. I apologize. I did not copy and paste my the code I used in my test file into my email. So you are right, I was using it in an echo statement and not during assignment. That's why you got the parse error. Chris. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Freelance code optimizations
I have written an online event registration system and I am curious to know if there is anyone out there who could either take my code and optimize it for me or basically help me learn how to optimize my code off-list. I am a php nub and I know there are probably all kinds of bugs and duplications in my code but I think I have coded it free of all errors, warnings and notices. I would rather someone help me learn basic optimization techniques like functions and such off list because I would hate to have to keep spending money to have my code optimized I am a somewhat slow learner in that I cannot learn from reading - the stuff on functions, for example, is confusing to me - I can only learn by examples with explanations of why certain things are done. If ya'll think I will be too much of a pain to help, I will understand - but I would like to learn == Jay Fitzgerald, Design Director - Certified Professional Webmaster (CPW-A) - Certified Professional Web Designer (CPWDS-A) - Certified Professional Web Developer (CPWDV-A) - Certified E-Commerce Manager (CECM-A) - Certified Small Business Web Consultant (CWCSB-A) Bayou Internet - http://www.bayou.com/ http://www.bayou.com Toll Free: 888.30.BAYOU (22968) Vox: 318.338.2034 / Fax: 318.338.2506 E-Mail: mailto:[EMAIL PROTECTED] [EMAIL PROTECTED] ICQ: 38823829 / AIM: bayoujf / MSN: bayoujf / Yahoo: bayoujf ==
Re: [PHP] Freelance code optimizations
* Thus wrote Ray ([EMAIL PROTECTED]): so if $a is something like the size of an array or another thing that affects the number of times the loop needs to be run. then if you can, reverse the order of the loop so you can compare to 0 (or another constant) ?php $a = 1000; $i = $a; while( $i-- 0 ){} ? real 0m7.111s user 0m6.870s sys 0m0.040s Interesting approach... yet: My results for above 31.017u 0.007s 0:31.14 99.5%1544+918k 0+0io 0pf+0w You can optimize it some more: ?php $a = 1000; $i = $a; while( $i-- ){} ? these results 19.008u 0.031s 0:19.58 97.1%1544+918k 0+0io 0pf+0w Curt -- I used to think I was indecisive, but now I'm not so sure. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Freelance code optimizations
In that case it makes perfect sense since a concatenation is not actually performed. The loops stuff though might be more tricky to determine reasoning. I imagine it has to do with the execution tree and references being created at various stages. Probably the for loop has an extra step or two which over a large loop can produce noticeable results. Cheers, Rob. On Mon, 2003-08-11 at 14:06, Chris W. Parker wrote: Robert Cummings mailto:[EMAIL PROTECTED] on Monday, August 11, 2003 10:55 AM said: Or are you mistaken in assuming the following works when perhaps you use this style as part of an echo statement normally. I apologize. I did not copy and paste my the code I used in my test file into my email. So you are right, I was using it in an echo statement and not during assignment. That's why you got the parse error. Chris. -- .-. | Worlds of Carnage - http://www.wocmud.org | :-: | Come visit a world of myth and legend where | | fantastical creatures come to life and the | | stuff of nightmares grasp for your soul.| `-' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Freelance code optimizations
Robert Cummings mailto:[EMAIL PROTECTED] on Monday, August 11, 2003 11:11 AM said: $i = 0; while( $i++ 1000 ){} [snip] Notice the large difference when the incrementation occurs within the while check itself. Wow, look at that. I had no idea you could even do that. Now my pages will be *THAT* much faster. ;) Thanks. Chris. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Freelance code optimizations
also, if you can compare to a constant, there is a notable difference as well. ?php $i = 0; $a = 1000; while( $i++ $a ){} ? real0m10.268s user0m10.000s sys 0m0.030s ?php $i = 0; $a = 1000; while( $i++ 1000 ){} ? real0m7.057s user0m6.880s sys 0m0.020s so if $a is something like the size of an array or another thing that affects the number of times the loop needs to be run. then if you can, reverse the order of the loop so you can compare to 0 (or another constant) ?php $a = 1000; $i = $a; while( $i-- 0 ){} ? real0m7.111s user0m6.870s sys 0m0.040s YMMV On Monday 11 August 2003 13:10, Robert Cummings wrote: On Mon, 2003-08-11 at 13:29, Chris W. Parker wrote: * while loop $ctr = 0; $val = 0; while($ctr10) { $val++; $ctr++; } ** for loop $val = 0; for($ctr=0;$ctr10;$ctr++) { $val++; } I get the following results (very consistently +/- .1) which agrees with your results and also shows an improvement over your own optimization. for( $i = 0; $i 1000; $i++ ){} time php foo.php Content-type: text/html X-Powered-By: PHP/4.3.2 4.42user 0.03system 0:04.60elapsed 96%CPU (0avgtext+0avgdata 0maxresident)k 0inputs+0outputs (528major+216minor)pagefaults 0swaps --- $i = 0; while( $i 1000 ){ $i++; } time php fee.php Content-type: text/html X-Powered-By: PHP/4.3.2 4.22user 0.01system 0:04.43elapsed 95%CPU (0avgtext+0avgdata 0maxresident)k 0inputs+0outputs (527major+216minor)pagefaults 0swaps --- $i = 0; while( $i++ 1000 ){} time php foo.php Content-type: text/html X-Powered-By: PHP/4.3.2 2.97user 0.00system 0:03.09elapsed 96%CPU (0avgtext+0avgdata 0maxresident)k 0inputs+0outputs (526major+216minor)pagefaults 0swaps --- Notice the large difference when the incrementation occurs within the while check itself. Cheers, Rob. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php