The following code to find prime numbers greatly differs between Adobe ColdFusion (10) and Lucee (4.5) regarding performance. Tested on the same machine (6C i7 3930k @ 4 GHz, Windows 10 64 Bit, JVM memory settings equal on both CFML engines: JDK7 -Xms512m -Xmx2048m -XX:MaxPermSize=512m
):
<cfscript>
ticks = getTickCount();
stopIndex = 10000;
primes = [];
divisions = 0;
primes.add(2);
primes.add(3);
n = 5;
for (n; n < stopIndex; n += 2) {
isPrime = true;
d = 3;
for (d; d < n; d++) {
divisions++;
if (n % d == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
primes.add(n);
}
}
ticks = (getTickCount() - ticks);
</cfscript>
<cfoutput>
<p>
#numberFormat(divisions)# divisions in #ticks# ms.
</p>
<p>
#numberFormat(arrayLen(primes))# prime numbers found below #numberFormat(stopIndex)#.
</p>
</cfoutput>
stopIndex @ 10k
stopIndex @ 20k
stopIndex @ 30k
trycf.com
and cflive.net
show a similar gap.
I checked if cfscript (vs. tags) has impact on the time, it doesn't. CFML engine related server settings do not seem to have any noticable impact either.
What could be the reason for the performance difference?
And how could I possibly resolve this?
Background: I'm running heavy math ops (geometry, image rendering)) on a production server, which happens to be running Lucee, and noticed the sluggish performance.
Integer maths is slower than floating point maths, so this runs slower because of the way that Lucee stores variables as types. if you force the n to be non integer Lucee runs 4x faster
if ((n+1.0) % d == 0) {
This tweak also more than doubles the speed in ACF too
https://luceeserver.atlassian.net/browse/LDEV-1541
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With