Performance of Calculating Prime Numbers

This section provides a tutorial example on how to measure the execution performance of a JSP page that does prime number calculation.

The first area I want to test for performance is integer arithmetic calculations. The following JSP page calculates prime number starting from number 3, and repeats the test many times.

```<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.3">
-->
<jsp:directive.page contentType="text/html"/>
<jsp:directive.page import="java.util.*"/>
<jsp:scriptlet><![CDATA[
int[] primes = new int;
int numberOfTests = 100;
int numberOfPrimes = 1000;
long t1 = System.currentTimeMillis();
for (int nTest=1; nTest<=numberOfTests; nTest++) {
// Getting prime numbers
int nPrime = 0;
int i = 2;
while (nPrime < numberOfPrimes) {
i = i + 1;
int j = 2;
boolean isPrime = true;
while (j<i && isPrime) {
isPrime = i % j > 0;
j = j + 1;
}
if (isPrime) {
nPrime = nPrime + 1;
primes[nPrime-1] = i;
}
}
}
long t2 = System.currentTimeMillis();
long t = t2 - t1;

// Displaying the results
out.println("<html><body>");
out.println("<b>Performance Information:</b><br/>");
out.println("Number of tests = " + numberOfTests + "<br/>");
out.println("Time = " + (t/1000) + " seconds.<br/>");
out.println("<b>" + numberOfPrimes + " prime numbers:</b><br/>");
for (int n = 1; n <= numberOfPrimes; n++) {
out.println(primes[n-1] + ", ");
}
out.println("</body></html>");
]]></jsp:scriptlet>
</jsp:root>
```

I run this page, and got the following result. It tells me the page is working correctly.

```Performance Information
Number of tests = 1
Time = 0 seconds
First 1000 Prime numbers:
3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, ...
```

By changing the controlling parameters, I collected some execution times and listed below in comparison with other technologies on my old computer 2003:

```        Number     Number      Debug   Time
Cases   of Tests   of Primes   Mode    (sec)   Notes

1.      1          1000        Yes     25      ASP with IIS 5.0
2.      1          1000        No      25      ASP with IIS 5.0
3.      1          1000        ?        0      JSP with Tomcat 4.1.18
4.      10         1000        ?        2      JSP with Tomcat 4.1.18
5.      100        1000        ?       22      JSP with Tomcat 4.1.18
6.      100        1000        ?       21      JVM HotSpot 1.3.1
```

So this tells us that JSP pages are 100 times faster than ASP pages for integer calculations.

On my new computer in 2012, of course the sample JSP runs much faster. Execution time reduced from 22 seconds to 1 second for the 100x1000 test.

```        Number     Number      Debug   Time
Cases   of Tests   of Primes   Mode    (sec)   Notes

1.      1          1000        ?        0      JSP with Tomcat 7.0.32
2.      10         1000        ?        0      JSP with Tomcat 7.0.32
3.      100        1000        ?        1      JSP with Tomcat 7.0.32
4.      1000       1000        ?       18      JSP with Tomcat 7.0.32
```

On my second new computer 2018, the sample JSP runs a little bit faster. Execution time reduced from 18 seconds to 15 second for the 1000x1000 test. So the last 6 years, computer technology only improved by 17%.

```        Number     Number      Debug   Time
Cases   of Tests   of Primes   Mode    (sec)   Notes

1.      10         1000        ?        0      JSP with Tomcat 9.0.12
2.      100        1000        ?        1      JSP with Tomcat 9.0.12
3.      1000       1000        ?       15      JSP with Tomcat 9.0.12
4.      10000      1000        ?      159      JSP with Tomcat 9.0.12
5.      1000       1000        ?       15      JVM HotSpot 18 (JDK 10)
```