When does Common subexpression elimination apply?

Viewed 92
for (Pair p : pairs) {
    double f = foo(p)
    ...
}

foo() performs a simple mathematical calculation as follows:

double foo(Pair p) {
    return Math.cos(p.x) + Math.sin(p.y);
}

If all the items in pairs are the same, can the Java compiler optimise this using Common subexpression elimination? Or is there another optimisation that occurs? I am asking since I have found a significant time reduction when more values in pairs are the same.

0 Answers
Related