Is there any way to optimize this code?

Asked

Viewed 128 times

7

How to optimize this code to make it faster?

if (strpos($qt, "blood") !== FALSE){
  if (preg_match("/^blood (?<blood>.+)$/", $qt, $match)){
    switch ($match['blood']) {
      case "a+": 
        $result = "A+"; 
        $sndline = "Ideal donor: A+<br>Other donors: A+ or O+<br>Only if no Rh(+) found: A- or O-"; 
        break;
      case "a-": 
        $result = "A-"; 
        $sndline = "Ideal donor: A-<br>Other donors: A- or O-"; 
        break;
      case "b+": 
        $result = "B+"; 
        $sndline = "Ideal donor: B+<br>Other donors: B+ or O+<br>Only if no Rh(+) found: B- or O-"; 
        break;
      case "b-": 
        $result = "B-";
        $sndline = "Ideal donor: B-<br>Other donors: B- or O-"; 
        break;
      case "ab+": 
        $result = "AB+"; 
        $sndline = "Ideal donor: AB+<br>Other donors: AB+ or A+ or B+ or O+<br>Only if no Rh(+) found: AB- or A- or B- or O-"; 
        break;
      case "ab-": 
        $result = "AB-"; 
        $sndline = "Ideal donor: AB-<br>Other donors: AB- or A- or B- or O-"; 
        break;
      case "o-": 
        $result = "O-"; 
        $sndline = "Ideal donor: O-<br>Other donors: O-"; 
        break;
      case "o+": 
        $result = "O+"; 
        $sndline = "Ideal donor: O+<br>Other donors: O+<br>Only if no Rh(+) found: O-"; 
        break;
    }
  }
}
  • What is the purpose of this code? what content is in the variable $qt? letters, numbers, special characters, etc..

  • you ever heard of design patterns? thought of creating a Factory?

  • @qmechanik The variable $qt can get all this you said, but in this case, she gets the text: blood a+.

  • 1

    @Israelzebulon I’ve never heard of it (I’m still new to PHP). Could you give me an example?

  • @hsbpedro The variable Qtpode vir nesse formatotexto_blooda+_outrotextobla`, no spaces? or there will always be spaces?

  • @qmechanik What I am creating is a text interpreter. In this case, I want when the user type blood <...>, the interpreter takes what is in the group ... and print the message in question.

Show 1 more comment

1 answer

4


One way to optimize this code is to eliminate the use of regular expressions, because it involves the process of implanting the engine of the regex in string, what causes a overload, unless it is really necessary to use it, by the way, the PHP page of function preg_match, mentions:

Do not use preg_match() if you just want to check if a string appears in another string. Use strpos() or strstr() instead, they will be faster.

One way to do this is to use the function strpos to find the position of a value in the string and with the function substr, extract it:

function extrairPedaco($texto, $inicio, $delimitador){
    $sub = substr($texto, strpos($texto, $inicio) + strlen($inicio), strlen($texto));
    return substr($sub, 1, strpos($sub, $delimitador));
}

And to use it, do so:

$texto = "The blood A+ tend to be cooperative, sensitive, clever, passionate and smart.";
$referencia = "blood";
$tipos = ['+', '-'];

foreach ($tipos as $tipo){
    $pedaco = extrairPedaco($texto, $referencia, $tipo);
    $pedaco = strtoupper($pedaco); // Converte para maiúsculo, eliminando a necessidade de usar a variável "result".
        switch($pedaco){
            case "A+": 
                echo "Ideal donor: A+<br>Other donors: A+ or O+<br>Only if no Rh(+) found: A- or O-"; 
                break;
            case "A-":  
                echo "Ideal donor: A-<br>Other donors: A- or O-"; 
                break;
            case "B+":  
                echo "Ideal donor: B+<br>Other donors: B+ or O+<br>Only if no Rh(+) found: B- or O-"; 
                break;
            case "B-":  
                echo "Ideal donor: B-<br>Other donors: B- or O-"; 
                break;
            case "AB+": 
                echo "Ideal donor: AB+<br>Other donors: AB+ or A+ or B+ or O+<br>Only if no Rh(+) found: AB- or A- or B- or O-"; break;
            case "AB-": 
                echo "Ideal donor: AB-<br>Other donors: AB- or A- or B- or O-"; 
                break;
            case "O-":  
                echo "Ideal donor: O-<br>Other donors: O-"; 
                break;
            case "O+":  
                echo "Ideal donor: O+<br>Other donors: O+<br>Only if no Rh(+) found: O-"; 
                break;
            default:
                // Faça algo aqui caso as comparações acima falhem.
                echo "No blood found\n";
                break;
        } 
}

Exemplo

  • 1

    Thank you, Qmechanik!

  • 1

    doesn’t need the !empty($pedaco) because it will fall into the default of the switch and "Ideal donor: " repeats in all sentences could be initialized before the switch and concatenated using $sndline .= '...';

  • @Gabrielgartz Thanks for the suggestion.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.