JSP and JSTL Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 3.09, 2006

JSP Performance

Part:   1  2 

JSP/JSTL Tutorials - Herong's Tutorial Notes © Dr. Herong Yang

Using Cookies

Using JavaBean Classes

HTTP Response Header Lines

Non ASCII Characters

JSTL and Expression Language

File Upload

Execution Context

JSP Elements

JSP Standard Tag Libraries (JSTL)

JSP Custom Tag

... Table of Contents

Calculating Prime Numbers

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="1.2">
<!-- PrimeNumbers.jsp
     Copyright (c) 2003 by Dr. Herong Yang
-->
<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>Performace 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.

Performace 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 was able to get some results:

        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.

(Continued on next part...)

Part:   1  2 

Dr. Herong Yang, updated in 2006
JSP and JSTL Tutorials - Herong's Tutorial Notes - JSP Performance