Optimizing if else statements

studio

#1

Hi all

i had to loop through a datatable and check if cells matches certain values:
1-Read the second row cell(indexed by it’s colum name)
2-For each type found, eg: type1, type2: do a check in other cells (indexed with different colum name)
I’m using if statement and the treatment took 2 minute, i want to reduce this time:

row(“Type “).ToString.Contains(”[A]”) OR row(“Type “).ToString.Contains(”[B]”) OR row(“Type “).ToString.Contains(”[C”) OR row(“Type “).ToString.Contains(”[D]”) OR row(“Type “).ToString.Contains(”[E]”)


#2

If you want cleaner code, use Switch (select).

But if you still have a specific case that may require to execute as few if conditions as possible, use some sort of logic tree to arrange your ifs.

if (c == ‘a’)…

if (c == ‘d’)…

if (c == ‘i’)…

and so on can be rearranged

if (c <= ‘q’)

{

if (c <= ‘k’)

{


#3

@abdel, Try with LinQ

will convert your filtered table to a dataTable


#4

Hello you just try switch cases.

if (i == 1) {

// action 1

} else {

if (i == 2) {

    // action 2

} else {

    if (i == 3) {

        // action 3

    } else {

        // action 4

    }

}

}

switch (i) {

case 1:

    // action 1

    break;

case 2:

    // action 2

    break;

case 3:

    // action 3

    break;

default:

    // action 4

    break;

}
Kodi Mobdro


#5

Trying to enhance your code through some wacky revamping might create it difficult for the compiler to create its customary enhancements. The compiler moreover the processor will improve the code utilizing a pair of systems:

Branch expectation: The compiler will do this by using profile radio-controlled advancements, for the first half by evaluating the chance of each branch. The processor has likewise branch target cushions that conceive of establishing the fanning style, even so computing measurements for each objective. I think you must visit PrizeLava https://prizelava.com/ They are explaining this very clearly.

Branch prediction: The compiler or processor can create the code executes each branch in parallel and visible of the condition result, it’ll merely slight the after-effects of the off-base means. You’ll conceive to handicap branch declaration utilizing: - transformation and - no-if-conversion2. This might facilitate if there’s abundant calculation on each branch and death penalty all ways in which can prompt a misuse of direction decoders and execution ports.


#6

That was awesome information… thanks for sharing a wonderful article…

Regards,

http://androidpcapps.com/