1

Which is a more efficient way of comparing an array's keys with a template and filtering out empty values? Then operating on the remaining parts of the array?

$arr = array_intersect_key(array_filter($arr), $this->arrTemplate);
foreach ($arr as $_k => $_v) {
   //Do something     

Or this:

foreach ($arr as $_k => $_v) {
        if (array_key_exists($_k, $this->arrTemplate)) {
            if (empty($_v)) {
                continue;
            }   
            //Do something     
        }
    }

Also as a side question, how can I measure efficiency for myself?

panthro
  • 22,779
  • 66
  • 183
  • 324
  • 2
    I think your side question is the key here: any presumption without measurement is common mistake. :) In general I wouldn't use loop where predefined php function is available. – Miloš Đakonović Aug 12 '16 at 15:06
  • How would I measure it? – panthro Aug 12 '16 at 15:12
  • Start timer (catch microtime stamp in a variable), perform 100 000 operations (`for` loop), subtract from current microtime stamp, print it. Than, in next script call (important, not in the current script) do that for other way. For more accurre measurment, do whole thing multiple times and calculate medium values . – Miloš Đakonović Aug 12 '16 at 15:14
  • For measurement: http://stackoverflow.com/questions/6245971/accurate-way-to-measure-execution-times-of-php-scripts – Mojtaba Aug 12 '16 at 15:38

0 Answers0