PHP performance $ _GET

Is one option usually faster than the other?

Option A: $var = $_GET['param']; and then the $var link to the script.
Option B: Link $_GET['param'] in each case via a script.

Thanks!

+4
source share
6 answers

The difference in performance, if any of them will be insignificant and not worth the effort - so much so that even if you try to run the firmware, the difference will be consumed by random factors.

However, option A is better in terms of creating more robust code. for example, if you ever want to change the value of $var , you can do it from one position in your code.

Simply put, this will never become a bottleneck in your program.

+7
source

Compared to 10000000 cycles

 $var = "bleh"; for ($i=0; $i<10000000; $i++) { strlen($var); } 

and

 $array = array(); $array['blah'] = "bleh"; for ($i=0; $i<10000000; $i++) { strlen($array['blah']); } 

Results:

  • Var: 8.563s - 856.3 nanoseconds per loop.
  • Array: 8.699 s - 869.9 nanoseconds per loop.

1.6% speed difference.

+2
source

This type of micro-optimization is rarely worth thinking about. As a rule, it is better to think about how best to create your own code, and leave such problems to people who write the runtime of this language.

There are many factors that would be important if we really wanted to work, which was more effective:

  • how array access works to execute ['param'] one or more times
  • how superglobals like $_GET are implemented
  • how the assignment is implemented (which in PHP uses "copy on write")
  • how do you pass $var around your code (e.g. as a function parameter) and how is it implemented
  • how often do you need to read from a variable
  • how often do you write a variable

I am sure there are many more; and, of course, both your program and the PHP runtime in which it runs can be changed at all these points.

+2
source

Consistent with those that say you worry about the wrong level of optimization. (Although, if you really want to get hung up on such things, check out http://www.phpbench.com/ )

Your first level of optimization is to optimize the readability and maintainability of the code. Human clock cycles are much more valuable and expensive (generally speaking) than computer clock cycles. As an added bonus, you will be very pleasantly surprised how often the optimization of a person’s clock cycles will also make the code much faster.

I highly recommend Pure Code by Robert C. Martin for an excellent coding style guide.

+1
source

A:

 $var = $_GET['param']; // and then referencing $var through the script. 

A constant time equal to zero in computer science is required.

B: Link

 $_GET['param']; // in each occurrence through the script. 

This is an ugly approach and is performed only by "sloppy" programmers.

Let's say that you need to use $_GET['param'] 10 times on one page or on several pages, and then you decide that you want to change the parameter 'param' to 'p' or 'parameter', then you're out of luck.

You want to initialize the "magic numbers" and "magic values" of the variable (or constant) that you can edit later, and the programmer (regardless of whether you want it or someone else) does not know exactly what it is.

Imagine if programmers will use

 if($number == 3.1415) 

instead

 if($number == $pi) 

it comes down to a similar basis.

So no, no option is usually theoretically faster, but option B is going to give you hell later and ugly.

0
source

Use $ _GET ['param'] when you can. PHP must remember $ _GET ['param']. Having made a variable equal to $ _GET ['param'], PHP should now remember two variables that are exactly the same.

If you are not going to change the value of the new variable, it is faster to use $ _GET ['param']. (Keep in mind, some people like to make new variables just for readability)

-2
source

All Articles