[prev] [index] [next]

Performance Example (cont)

These considerations lead to the following code:

int is_prime(int n)
{
    int i, limit = sqrt(n);
    if (n == 1) return 0;
    if (n == 2) return 1;
    if (n % 2 == 0) return 0;
    for (i = 3; i <= limit; i+=2) {
        if (n % i == 0) return 0;
    }
    return 1;
}

But code is now less clear, performance gains are marginal.
This is generally a sign to stop tuning.