littledragon Posted December 20, 2007 Share Posted December 20, 2007 Hi all Consider this. We have an array with 2 values in it - the first value [ i ] is a small integer, the second [string] is an enooooormous string, like so: <?php $array[i] = 1; $array[string] = 'A very long string indeed that goes on for pages and pages etc...'; ?> Now consider these 2 possible methods of writing a function and passing variables to it. <?php // scenario 1 ---------------- $val = get_val($array[i]); function get_val($integer){ return $integer; } // scenario 2 ---------------- $val = get_val($array); function get_val($array){ return $array[i]; } ?> The 2 scenarios are identical in that both will return the same value, but I'm lacking the knowledge of low-level programming to tell whether the second scenario is less efficient than the first. We talk about variables being "passed" to functions (as if variables are copied into a function - right now my own speculation leads me to believe this the logical scenario) whereas when I think more mathematically I imagine a function being "applied" to variables (i.e. no extra memory needed since those variables already exist in memory anyway). Sometimes I arrive at one conclusion, sometimes the other ??? Any thoughts greatly appreciated. If my question needs any more explaining let me know (let me know which bit you don't get) Quote Link to comment https://forums.phpfreaks.com/topic/82498-solved-memory-efficient-coding-practice-thoughts-please/ Share on other sites More sharing options...
eZe616 Posted December 20, 2007 Share Posted December 20, 2007 How so will they return the same value? In scenario one you're just returning the value you already have. In which point the function isn't necessary and you can just write $val = array[$i] In the second scenario, your passing the array, but returning the same value probably every time. In which point I don't see the need for a function also. Quote Link to comment https://forums.phpfreaks.com/topic/82498-solved-memory-efficient-coding-practice-thoughts-please/#findComment-419406 Share on other sites More sharing options...
littledragon Posted December 20, 2007 Author Share Posted December 20, 2007 Yes, I know, the function is as simple as possible to illustrate the point. The functions themselves could be anything, and the array I give in the first code snippet is the one to be used in both scenarios. The question is about memory efficiency. Quote Link to comment https://forums.phpfreaks.com/topic/82498-solved-memory-efficient-coding-practice-thoughts-please/#findComment-419407 Share on other sites More sharing options...
JasonLewis Posted December 20, 2007 Share Posted December 20, 2007 scenario one, like stated above, is returning "1". they are really wasting space those functions. so yeh, you've kinda confused me. edit, in fact both functions seem kinda pointless... unless they are for a bigger picture..... Quote Link to comment https://forums.phpfreaks.com/topic/82498-solved-memory-efficient-coding-practice-thoughts-please/#findComment-419409 Share on other sites More sharing options...
littledragon Posted December 20, 2007 Author Share Posted December 20, 2007 yes it will, look again $array[i] = 1; $array[string] = 'A very long string indeed that goes on for pages and pages etc...'; // scenario 2 ---------------- $val = get_val($array); function get_val($array){ return $array[i]; } Quote Link to comment https://forums.phpfreaks.com/topic/82498-solved-memory-efficient-coding-practice-thoughts-please/#findComment-419411 Share on other sites More sharing options...
JasonLewis Posted December 20, 2007 Share Posted December 20, 2007 it will, replacing the values that are currently there with proper values. then of course it will work... but what are the point of these functions, you can return the values without the use of them. Quote Link to comment https://forums.phpfreaks.com/topic/82498-solved-memory-efficient-coding-practice-thoughts-please/#findComment-419414 Share on other sites More sharing options...
littledragon Posted December 20, 2007 Author Share Posted December 20, 2007 ProjectFear (edit - thought replies were all from one person - sorry ProjectFear), the functions don't serve any useful purpose other than being as simple as they possibly can be, they are simply an illustration. This isn't a scripting question. It's... The question is about memory efficiency. Quote Link to comment https://forums.phpfreaks.com/topic/82498-solved-memory-efficient-coding-practice-thoughts-please/#findComment-419419 Share on other sites More sharing options...
JasonLewis Posted December 20, 2007 Share Posted December 20, 2007 haha well.. you've confused me :S Quote Link to comment https://forums.phpfreaks.com/topic/82498-solved-memory-efficient-coding-practice-thoughts-please/#findComment-419422 Share on other sites More sharing options...
MadTechie Posted December 20, 2007 Share Posted December 20, 2007 i am guessing your asking if its more memory efficient to pass the whole array and then the function pulls out the "correct" item and uses it ~or~ workout the correct item then only pass that to the function to use! if this is what your asking then i think passing the item would be better but it would be even better to setup a class and set it as public array and refer to that.. hope that helps EDIT: typos Quote Link to comment https://forums.phpfreaks.com/topic/82498-solved-memory-efficient-coding-practice-thoughts-please/#findComment-419429 Share on other sites More sharing options...
littledragon Posted December 20, 2007 Author Share Posted December 20, 2007 Thanks madtechie Yeh, instincts tell me passing the single value would be better (or at least harmless), but is it actually inefficient to pass the whole array? Quote Link to comment https://forums.phpfreaks.com/topic/82498-solved-memory-efficient-coding-practice-thoughts-please/#findComment-419432 Share on other sites More sharing options...
trq Posted December 20, 2007 Share Posted December 20, 2007 Your nit picking over something that in reality will have no effect on the efficiency of an application. PHP handles variables very well, this is simply not worth thinking about. You should have bigger fish to fry. Quote Link to comment https://forums.phpfreaks.com/topic/82498-solved-memory-efficient-coding-practice-thoughts-please/#findComment-419436 Share on other sites More sharing options...
JasonLewis Posted December 20, 2007 Share Posted December 20, 2007 Your nit picking over something that in reality will have no effect on the efficiency of an application. PHP handles variables very well, this is simply not worth thinking about. You should have bigger fish to fry. ...words of a master... Quote Link to comment https://forums.phpfreaks.com/topic/82498-solved-memory-efficient-coding-practice-thoughts-please/#findComment-419440 Share on other sites More sharing options...
littledragon Posted December 20, 2007 Author Share Posted December 20, 2007 thorpe - sometimes I totally agree with what you just said, sometimes I don't. Still not convinced one way or the other... if I can pass the array, no matter how humungus it is, cos it makes no difference at all, that's what I'm going to start doing... edit - what I really want to know is, is there anyone here who abso-freakin-lutely knows the answer for sure? opinions still welcome Quote Link to comment https://forums.phpfreaks.com/topic/82498-solved-memory-efficient-coding-practice-thoughts-please/#findComment-419441 Share on other sites More sharing options...
MadTechie Posted December 20, 2007 Share Posted December 20, 2007 benchtesting time.. create a massive array (use a loop) then pass ie into a function 40000000 times (with a returning the same var) check memory usage and execution time.. i'll love to know Quote Link to comment https://forums.phpfreaks.com/topic/82498-solved-memory-efficient-coding-practice-thoughts-please/#findComment-419444 Share on other sites More sharing options...
littledragon Posted December 20, 2007 Author Share Posted December 20, 2007 MadTechie - brilliant, why didn't I think of that? benchtesting - OK, but questions (never done it before)... memory usage and execution time execution time OK, memory usage how? 40000000 times is that number significant? No it isn't is it... I'm just being dumb... Quote Link to comment https://forums.phpfreaks.com/topic/82498-solved-memory-efficient-coding-practice-thoughts-please/#findComment-419445 Share on other sites More sharing options...
littledragon Posted May 21, 2008 Author Share Posted May 21, 2008 In case anyone got to this by googling... I have a much better understanding of how php handles memory now, and an array _will_ be duplicated in memory when you pass it into a function. The way around this (if the array is large enough to worry about) is to pass only the array key you want, or take a (slightly) more object-oriented approach and simply use $GLOBALS['array'] / global $array to prevent duplication (if you're not going to affect the array / you wanted to affect it anyway). If the array ain't that big, it's probably not a big deal (as many people above pointed out to me) but if it's the results of a * query on a sizeable sql db then you start to think about this kind of thing ... Regards all, enjoy this great site ld Quote Link to comment https://forums.phpfreaks.com/topic/82498-solved-memory-efficient-coding-practice-thoughts-please/#findComment-546540 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.