Is it better to call a function in a nested way or to separate each pass inside var?

Take these three examples:

ONE

return lowercaseKeys(json_decode(trim($json),true)); 

TWO STRONG>

 $trimmed = trim($json); $array = json_decode($trimmed,true); $return = lowercaseKeys($array); return $return; 

THIRD

 $return = trim($json); $return = json_decode($return,true); $return = lowercaseKeys($return); return $return; 

Beyond readability, which is best for evaluating performance? What is considered best practice?

ps the code is just an example, not related to the question, I just copied it from the window of my notebook.

+6
php
source share
8 answers

Rule number one does what is most readable when working with micro-optimizations. But here is a little test that I did.

 <?php $iterations = 1000000; $tests = array('one', 'two', 'three'); $json = json_encode($tests); foreach ($tests as $function) { echo $function; $start = microtime(true); for ($i = 1; $i <= $iterations; $i++) { $function($json); } $end = microtime(true); echo ' - ' . ($end - $start) . " sec\n"; } function one($json) { return array_change_key_case(json_decode(trim($json),true), CASE_LOWER); } function two($json) { $trimmed = trim($json); $array = json_decode($trimmed,true); $return = array_change_key_case($array, CASE_LOWER); return $return; } function three($json) { $return = trim($json); $return = json_decode($return,true); $return = array_change_key_case($return, CASE_LOWER); return $return; } ?> 

Results:

 one - 3.3994290828705 sec two - 3.5148930549622s sec three - 3.5086510181427s sec 

Option one is actually a tiny bit faster, but it was a million iterations, and the time difference was still not even noticeable. With fewer iterations, there is too much time to even have a template to declare it better than another.

+2
source share

Option 1 will probably be a microsecond faster than the other 2 options and use a few bits less physical memory, but any difference will be completely insignificant if not multiplied by an exponential level. The core here is utmost readability. I believe that option 1 is ideal personally, but I admit that in some teams where I work, this option does not meet the standard for the lowest common developer denominator. Option 2 and Option 3 are really exactly the same, since you are not doing anything with the additional variables you create. Both of them create 3 separate tokens. Option 2 is more clearly read in that the variables describe the method used at this stage, so I will also vote for 2.

+1
source share

Performance wise, I don’t think you really noticed any difference. It would be insignificant ...

But I vote for option 2 because it is easiest to read (as opposed to option 1 ), and I don't think it is a good idea to rewrite it as you do in option 3 (not a very good form).

0
source share

There should be no performance difference in your example.

0
source share

If PHP does this correctly, they should all be the same after tokenized . Assuming the truth, the second option is the most readable and easiest to modify in the future. This page might be worth a read.

0
source share

Personally, I like option one, as for your questions:

which is best for work? Option 1

What is considered best practice? Option 1 and 2 depending on the complexity of the function.

But let's see how they look like a function

Option one:

 // Simple, readable function optionOne($json) { return lowercaseKeys(json_decode(trim($json),true)); } 

Option Two:

 // Still readable with a little more detail to a novice developer function optionTwo($json) { $trimmed = trim($json); $array = json_decode($trimmed,true); $return = lowercaseKeys($array); return $return; } 

Option Three:

 // This might cause some problems with the $return variable // Still looks like it would work but I'm not fond of this option function optionThree($json) { $return = trim($json); $return = json_decode($return,true); $return = lowercaseKeys($return); return $return; } 
0
source share

I usually do "option 2" myself. You never know when you might need something halfway. Helps with readability too. Well, this is not so effective, but in order to understand and edit your code at a later date (especially if someone needs it), option 2 suits me best.

0
source share

I like the first option, and it uses less memory, but the difference is not significant

0
source share

All Articles