Remix.run Logo
▲ gpderetta a day ago

https://en.wikipedia.org/wiki/Loop_unswitching

▲econ a day ago | parent [-]

    bool w;
    int x[1000];
    int y[1000];
    for(int i=0;i<1000;i++){
      x[i] += y[i];
    }
    if(w){
      for(int i=0;i<1000;i++){
        y[i] = 0;
      }
    }
Slower but prettier with the if pushed down.
▲bzbz 9 hours ago | parent [-]

Not particularly.

Easier for humans to reason about the branch, when they know that it happens without input from i or y[i].

This is helpful when the `if` statement is complex, or there are multiple if statements, some of which do take y[i] as input.