mardi 9 octobre 2018

Is better if statements with multiple conditions or more else if statements?

I'm mathematician so I don't know much about IT stuff. And I want to know if is faster to use statements with multiple conditions or more if/else if statements like in example below. Consider I have really big data table (with millions of rows) and have this if statements in function which I apply to each row of one column and the result is stored in new column. And I just want to find out if there is some difference (faster/slower/same) between this two approaches.

    if (is.na(numerator) == TRUE){
        result = 0
    }  else if (numerator == 0){
        result = 0
    }  else if (is.na(denominator) == TRUE){
        result = max
    }  else if (denominator == 0){
        result = max
    }  else {
        result = numerator/denominator
    }

OR

    if (is.na(numerator) == TRUE || numerator == 0){
        result = 0
    }  else if (is.na(denominator) == TRUE || denominator == 0){
        result = max
    }  else {
        result = numerator/denominator
    }

Aucun commentaire:

Enregistrer un commentaire