Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prime number performance difference ACF vs. Lucee

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

  • ACF: 280 ms
  • LUC: 1300 ms

stopIndex @ 20k

  • ACF: 1000 ms
  • LUC: 4800 ms

stopIndex @ 30k

  • ACF: 2200 ms
  • LUC: 10500 ms

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.

like image 845
Alex Avatar asked May 04 '16 20:05

Alex


1 Answers

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

like image 195
zac spitzer Avatar answered Nov 02 '22 12:11

zac spitzer