[prev] [index] [next]

Performance Example (cont)

An "optimization" that doesn't help ...
  • we don't need to check 1 and n
  • so, eliminate them from the loop and change the test

int is_prime(int n)
{
    int i, ndiv = 0;
    for (i = 2; i < n; i++) {
        if (n % i == 0) ndiv++;
    }
    return (ndiv == 0);
}

No surprise ... the execution time hardly changes at all.