Jump to content

[SOLVED] Memory efficient coding practice - thoughts please


Recommended Posts

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)

 

 

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.

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.

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.....

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];
}

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.

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.

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

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... :P

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 :)

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...
  • 5 months later...

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.