0

1I know it may sound silly from the get go...

and let me tell you right off the batt, this ain't the same question as The advantage / disadvantage between global variables and function parameters in PHP. asked right here on stackoverflow. There, asker wonders local vars vs global vars. Here, globals vs globals. My question is all about the PHP's internal way of handling the global variable access and speed.

Here is the question, in the below examples, is the function_1 supposed to run faster than the function_2?

function function_1 ( &$global_variable_x) {
    //do something with $global_variable_x
}



function function_2 () {
    global $global_variable_x;
    //do something with $global_variable_x
}

Let me highlight what's the difference...

In case 1, you pass the global in the function arguments and not only that, you pass it as by ref so the memory location is handed to PHP directly. Because of this trick, there is no need for the use of the global keyword within the function, and because of this very fact, there is no time spent by PHP looking up the global in the global name space. Then the question is why not do it? It's got to be faster, ain't it?

Of course, it is easy to misinterpret this question and get into the usual chores of talking about

  1. Globals are bad
  2. Globals do not need to be passed thru function args because globals well... are globals, so they can be accessed anywhere anyway. and finally, it does not make sense to pass a global thru a function argument from a semantical point of view, it confuses the hell out of people.

none of which addresses the question being asked.

It's all about speed.

Community
  • 1
  • 1
Average Joe
  • 4,521
  • 9
  • 53
  • 81
  • 3
    I believe this is answered already... http://stackoverflow.com/questions/2216340/the-advantage-disadvantage-between-global-variables-and-function-parameters-in ? – niaccurshi Feb 28 '13 at 00:12
  • You should only use references (`&`) when you actually need references. *Never* use them to speed up your code. The cases where references used to speed up the code have been fixed, and PHP handles it automatically now. – Sverri M. Olsen Feb 28 '13 at 00:18
  • @niaccurshi Not exactly. They discuss snot exactly the same thing. The question there local var vs global var implementation. I've seen that post before I asked my question. Thank you for your ref though. – Average Joe Feb 28 '13 at 00:20
  • @SverriM.Olsen Thank you for your comment Olsen. But, that improvement has to do with the issue of `copy on write`as this article ( http://www.thedeveloperday.com/php-lazy-copy/ ) nicely covers it. My question probes a different point of view. – Average Joe Feb 28 '13 at 00:23
  • @AverageJoe Ah, okay. But in any case, is it really worth using your time on this? The speed difference is so small that it is probably not possible to even measure it. – Sverri M. Olsen Feb 28 '13 at 00:41

1 Answers1

0

if its global it makes no sense to use it as an argument to a function which can see that global. It either 1) won't be faster or 2) it will run barely slower 3) it will run faster by very little and the reason for this will defy formal logic.

Dmytro
  • 5,068
  • 4
  • 39
  • 50
  • Please see my disclaimers. Isn't what makes globals slow is the global key within the function that forces PHP to check if the global exists in the global namespace? If so, wouldn't avoiding the global key all together and giving the memory ref straight from the get go be twice faster? – Average Joe Feb 28 '13 at 00:17
  • If the variable is ALREADY global, then might as well use it. If the variable is passed from a local to the function, then you can't access that info any other way. Passing a global as a parameter to a function which sees that global will not be faster because you still have to obtain that global, except now you add an implicit local variable as well which stores the value of global. – Dmytro Feb 28 '13 at 00:22
  • when you say this `because you still have to obtain that global,`, I'm loosing you. Didn't I pass the global by reference? We are just giving PHP the exact memory address? So what's there to obtain? It's already obtained. And this is exactly point in my question. – Average Joe Feb 28 '13 at 00:30