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">
<!-- PrimeNumbers.jspx
 - Copyright (c) 2003-2018, HerongYang.com, All Rights Reserved.
-->
<jsp:directive.page contentType="text/html"/>
<jsp:directive.page import="java.util.*"/>
<jsp:scriptlet><![CDATA[
   int[] primes = new int[1000];
   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)

Last update: 2018.

Table of Contents

 About This Book

 JSP (JavaServer Pages) Overview

 Tomcat Installation on Windows Systems

 JSP Scripting Elements

 Java Servlet Introduction

 JSP Implicit Objects

 Syntax of JSP Pages and JSP Documents

 JSP Application Session

 Managing Cookies in JSP Pages

 JavaBean Objects and "useBean" Action Elements

 Managing HTTP Response Header Lines

 Non-ASCII Characters Support in JSP Pages

Performance of JSP Pages

Performance of Calculating Prime Numbers

 Performance of Loading JSP Pages

 EL (Expression Language)

 Overview of JSTL (JSP Standard Tag Libraries)

 JSTL Core Library

 JSP Custom Tags

 JSP Java Tag Interface

 Custom Tag Attributes

 Multiple Tags Working Together

 File Upload Test Application

 Outdated Tutorials

 References

 Full Version in PDF/EPUB