Page 2 of 3

Re: PHP optimization forum

Posted: Mon Apr 24, 2006 1:16 pm
by AKA Panama Jack
Oren wrote:Hi guys,

I have a good idea :idea:, what do you think about opening a PHP optimization forum where people post their code and then we all together combine forces and our knowledge in order to optimize the code and make it simple, small and fast as possible?
...
P.S It was just for the example and I'm not saying that using array_walk() is faster than using foreach() or vise versa - I really haven't got the chance to check it out :wink:
I like the idea. :D

I would hope people would post results showing how their code optimized things.

There are some times when what looks like SMALL code will actually use more memory or end up being slower than using a larger block of code. Just because you reduce the number of lines of code doesn't mean it is optimized. It just means you reduced the number of lines of code. :)

Remember optimization doesn't mean compacting your code to fit on as few lines as possible.

Optimization means creating code that uses less system resources (memory and CPU) and/or executes faster.

Posted: Mon Apr 24, 2006 1:48 pm
by Christopher
Would there be a forum rule that all posters would be responsible for going back, retesting and editing all their comments each time a new version of PHP is released?

Re: PHP optimization forum

Posted: Mon Apr 24, 2006 9:37 pm
by Roja
AKA Panama Jack wrote:Remember optimization doesn't mean compacting your code to fit on as few lines as possible.

Optimization means creating code that uses less system resources (memory and CPU) and/or executes faster.
Thats an arbitrary choice of a definition. What if you can use less system resources, but execute it slower? Or execute it faster, and fit on fewer lines, but use more memory?

Optimization depends on what your goals are, which is why a forum specific to it will be horribly confused before it gets started. At the very least, each poster would need to clarify what their specific goals are for that optimization (memory, cpu, lines, simplicity, testability, reusability).. optimization simply means to make better - in what sense is in the hands of the coder.

Posted: Mon Apr 24, 2006 9:45 pm
by RobertGonzalez
It would seem that optimization could be best defined as achieving the most optimum execution results of your app with your code. I suppose the idea would be to redevelop code until it does what it's supposed to do with the least amount of used resources and in the fastest amount of time. Sometimes these two do not coincide, so I would argue that optimization is finding that balance between the two that makes for the best possible execution circumstance.

Posted: Tue Apr 25, 2006 1:02 am
by Oren
I believe that the fastest code would probably use less system resources in most cases.

Posted: Tue Apr 25, 2006 3:31 am
by Benjamin
You guys are nuts, anything that uses less "system resources" is going to execute faster. Unless your talking about 1 app using disk io and another app not. If everything is all in ram or CPU registers then it's all in the code.

Posted: Tue Apr 25, 2006 10:45 am
by RobertGonzalez
agtlewis wrote:... anything that uses less "system resources" is going to execute faster.
I would agree with this. However, imagine if your code was written in a way that utilizes the same system reources over and over again in a way that is unneccesary. Like opening a database connection, running a query, then closing the connection, except doing this for each of the 52 queries your script is running. To me that is inefficient use of code and resource. So in this instance I would say that optimization of the code would be the cleaning up of inefficiencies with the code base. This is just my opinion.

Posted: Tue Apr 25, 2006 11:27 am
by feyd
The moderators have discussed the idea and we can't see a use in it right now. Sorry guys.

Posted: Tue Apr 25, 2006 11:33 am
by RobertGonzalez
It's all good. It's not like we can't talk about it somewhere else around here. Sheesh, there are a ton of different forums here. I'm sure we can pick one.

Posted: Tue Apr 25, 2006 12:41 pm
by Maugrim_The_Reaper
n00b, is your blog filled with invisible text? :)

On topic - not sure its needed. Problem is many of the low level optimisations are largely things you pick up through experience and rarely think about thereafter. I'd have difficulty mentioning more than a few off-hand - I probably do them without thinking. Other areas of optimisation are more a PHP General or Theory topic if they crop up...

Posted: Tue Apr 25, 2006 6:59 pm
by AKA Panama Jack
Everah wrote:It would seem that optimization could be best defined as achieving the most optimum execution results of your app with your code. I suppose the idea would be to redevelop code until it does what it's supposed to do with the least amount of used resources and in the fastest amount of time. Sometimes these two do not coincide, so I would argue that optimization is finding that balance between the two that makes for the best possible execution circumstance.
That's definately true. :D Roja was just being anal in trying to rebut what I wrote. ;)

It's always about a balance. Apparently you got what I was saying. :) Optimization is not about how small you can make your code look in your editor.

Posted: Tue Apr 25, 2006 7:43 pm
by Roja
agtlewis wrote:You guys are nuts, anything that uses less "system resources" is going to execute faster. Unless your talking about 1 app using disk io and another app not. If everything is all in ram or CPU registers then it's all in the code.
Not true. Take for example, two different approaches to trying to store 100,000 records to a db.

You can build one enourmous array, dump it all to the db (high memory use, but fast). Or you can make a 2,000 element array, dump it to the db, and then repeat until you get to 100k records. (lower memory use, but slower).

Its a real example - from BNT's make galaxy system.

There is often a choice between faster, and more memory use, and yes, even more lines of code used. Using the term "optimization" is unclear. Optimization means to make better. Which item would we be trying to make better?
PanamaJack wrote:That's definately true. Very Happy Roja was just being anal in trying to rebut what I wrote. Wink
I was being accurate. You were not, and now you are making ASSumptions instead of reading what I wrote. Lets stay on topic. Your definition of optimization is unclear and arbitrary.

Posted: Tue Apr 25, 2006 9:46 pm
by Benjamin
Roja wrote: You can build one enourmous array, dump it all to the db (high memory use, but fast). Or you can make a 2,000 element array, dump it to the db, and then repeat until you get to 100k records. (lower memory use, but slower).
Your example is correct, but I don't agree that it disproves what I said for a few reasons.

1. Using different types of arrays or db insertion techniques is in fact in alignment with my original comment "It's all in the code".
2. Reading/Writting to a DB is the same as Disk IO (for argumentative purposes) because the code is no longer in control of the speed as it's waiting for other processes to complete. Depending on database memory usage however, I would be willing to bet that if MySQL was cacheing the entire table(s) in ram, the speed difference probably would become less of an issue. In your example, what would the time difference be, between the two methods, if you subtract the DB Query time from both of them?
3. More weight should be given when calculating system resource usage to DISK IO, DB Connections, Network IO and things of that nature, rather than Memory usage.

Posted: Wed Apr 26, 2006 12:31 am
by RobertGonzalez
Roja wrote:I was being accurate. You were not, and now you are making ASSumptions instead of reading what I wrote. Lets stay on topic. Your definition of optimization is unclear and arbitrary.
This is starting to sound a little like the OOP vs Procedural thing that led to that thwarted coup d'etats 8O . The word 'optimization', in and of itself, is a vague and unclear term when used to define process improvements. Without knowing the elements of the process, the external variables to the process, the users of the process or the medium of the process you can't 'optimize' the process. So goes the case with code. That is why I am of the opinion that optimization, as it relates to this thread, can be understood as the balance of efficiency with the code you produce and what the code uses as it produces. This discussion is taking on a heck of a lot more than 'What do you think about a forum dedicated to optimization?'.

Any chance one of the mods would consider splitting this thread to allow the conversation to continue on it's own?

Posted: Wed Apr 26, 2006 7:35 am
by Roja
agtlewis wrote:Your example is correct, but I don't agree that it disproves what I said for a few reasons.
On the contrary, it direct refutes this comment:
agtlewis wrote:anything that uses less "system resources" is going to execute faster
You can produce code that uses MORE resources, and execute faster. You can also produce code that uses less resources, but executes slower. Thats what my example showed, and it can occur independent of db, file i/o, or anything similar. I simply used a real world example that really did use *more* resources to execute faster - which happened to contain a db call.

My point continues to be that optimization isn't a sufficient definition. "Make it better" doesn't specify what you are trying to improve. Size, speed, memory use, reusability, there are a number of things that can be improved on, and they are not - in contradiction to your claim - all going to do the same thing. There are times when code has to choose between them, and thats why discussing optimization means being specific about what the goals are.