Bug 7844 – implement loop invariant code motion for pure functions
Status
RESOLVED
Resolution
DUPLICATE
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
Linux
Creation time
2012-04-06T01:14:27Z
Last change time
2022-08-16T11:13:10Z
Assigned to
No Owner
Creator
minas_mina1990
Comments
Comment #0 by minas_mina1990 — 2012-04-06T01:14:27Z
I have recently made a program that finds the sum of all primes (they are 11) that truncable from left to right and from right to left.
I compiled it with -O -release -inline -noboundscheck
The results were:
real 0m1.659s
user 0m1.652s
sys 0m0.000s
I made a few changes to make it compile as C code, and compiled in gcc with -O5 -lm -std=c99. The results:
real 0m0.764s
user 0m0.760s
sys 0m0.004s
There's ~900 ms difference here, which is big.
The problem is in a function I used, "isPrime(ulong)"
/// returns true if n is a prime number
bool isPrime(ulong n)
{
// 0 and 1 aren't primes
if( n < 2 ) return false;
if( n % 2 == 0 && n != 2)
return false; // an even number can't be a prime (except 2)
// check only if it's odd
for(ulong i = 2; i <= cast (ulong)sqrt(cast(double)n+1); ++i)
if( n % i == 0 )
return false;
return true;
}
As you can see, in the for loop, sqrt() is called in every iteration and that's what makes it slower. When changed to:
ulong limit = cast (ulong)sqrt(cast(double)n+1)
for(ulong i = 2; i <= limit; ++i)
It performs in 400ms!!!
Somehow the C version manages to do that sort of optimizations. I expect DMD to do it as well, as it makes the code A LOT faster.
Comment #1 by timon.gehr — 2012-04-06T12:52:59Z
This optimization is called loop invariant code motion.
Comment #2 by razvan.nitu1305 — 2022-08-16T11:13:10Z
*** This issue has been marked as a duplicate of issue 4453 ***