-1

I would like to know what is the quickest and most effective way to make comparisons. This will be more meaningful with my code:

public function exampleChangeVariable ($variable) {

  if ($variable == 'notif_received_title') {
    $variable = _('Text 1');
  }
  else if ($variable == 'notif_inprogress_title') {
    $variable = _('Text 2');
  }
  else if ($variable == 'notif_preparation_title') {
    $variable = _('Text 3');
  }
  else if ($variable == 'notif_positif_title') {
    $variable = _('Text 4');
  }
  else if ($variable == 'notif_negatif_title') {
    $variable = _('Text 5');
  }
  else if ($variable == 'notif_accepted_title') {
    $variable = _('Text 6');
  }
  else if ($variable == 'notif_rejected_title') {
    $variable = _('Text 7');
  }

  return $variable;
}

This function is found in a foreach (data recovered in database) and I would like to optimize it to the maximum.

  • Should I instead use numbers as a variable?

  • If I want to use text variables, is length important? (notif_received_title would be received)

  • The else if is a good choice?

  • How could I do better? (knowing he could have about twenty if)

Rocstar
  • 1,427
  • 3
  • 23
  • 41
  • 1
    This doesn't look like code that would cause a bottleneck so optimizing it won't get much more out of it (if it gets anything). Using an array mapping would certainly make it cleaner though. – apokryfos Apr 25 '18 at 08:27
  • 1
    I say use a lookup array. The possible dupe isn't using keys and values like I'd recommend. But this is definitely a duplicate of many questions on this site. Please research before posting a question. – mickmackusa Apr 25 '18 at 08:28
  • IMHO Trying to come up with a generic - 'this way is best' answer is impossible without coming down to being an opinion. – Nigel Ren Apr 25 '18 at 08:29
  • I would also suggest if the data is coming from the database, that is likely to be the biggest performance problem and this is trivial in comparison. – Nigel Ren Apr 25 '18 at 08:32
  • This is a [tag:micro-optimisation]. The effect on performance would be negligible no matter what approach you used, unless you're processing literally millions of data points in a loop (though some methods might be easier for a programmer to read than others). – GordonM Apr 25 '18 at 08:34
  • So you think that no matter how I do it will not change much? Does reducing the size of the texts to be compared have an impact? – Rocstar Apr 25 '18 at 08:38
  • @Rocstar Yes, that is probably the biggest factor in how fast this will be. A 2gb file will take twice as long to process as a 1gb file. If at all possible you should split the file into chunks and process each one individually. On a multi-core system you can run the the processes simultaneously and get a much bigger performance win. – GordonM Apr 25 '18 at 08:47
  • @GordonM would you mind adding this link: https://stackoverflow.com/questions/16675753/php-fastest-way-to-handle-undefined-array-key please? – mickmackusa Apr 25 '18 at 08:47

1 Answers1

3

Actually, I'd just make an associative array and fetch the right index.

Your code would become :

public function exampleChangeVariable($variable)
    {

        $assoc = [
            'notif_received_title'    => 'Text 1',
            'notif_inprogress_title'  => 'Text 2',
            'notif_preparation_title' => 'Text 3',
            'notif_positif_title'     => 'Text 4',
            'notif_negatif_title'     => 'Text 5',
            'notif_accepted_title'    => 'Text 6',
            'notif_rejected_title'    => 'Text 7'
        ];

        if (array_key_exists($variable, $assoc)) {
            return $assoc[$variable];
        }

        return null;

    }

Although there would still be room for improvement ( since keys have similar patterns )

Aurelien
  • 1,497
  • 7
  • 15