Subj : Performance: Another test of Java vs JavaScript ... To : netscape.public.mozilla.jseng From : "Neil Kolban" Date : Thu Oct 16 2003 11:48 pm This is a multi-part message in MIME format. ------=_NextPart_000_0019_01C39437.A0589D60 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Folks, Here are a couple of code fragments.... the first is Java and the 2nd is = JavaScript running under Rhino. -------------------------------------------------------------------------= ------- public class Perf2 { public static void main(String[] args) { String string =3D "ABCDEFGHIJ"; String sub; int i =3D 1; long start =3D System.currentTimeMillis(); while (i<200000) { sub =3D string.substring(3,6); i =3D i + 1; } long diff =3D System.currentTimeMillis() - start; System.out.println("Elapsed: " + diff); } =20 } -------------------------------------------------------------------------= ------- function myFunc() { var string =3D "ABCDEFGHIJ"; var sub; var i; i =3D 1; while (i < 200000) { sub =3D string.substring(3, 6); i =3D i + 1; } } myFunc(); -------------------------------------------------------------------------= ------- The Java program takes 15 milliseconds, the JavaScript takes 850 = milliseconds. I understand compiled Java will probably be faster but I = am surprised at how much faster. ------=_NextPart_000_0019_01C39437.A0589D60 Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
Folks,
 
Here are a couple of code fragments.... = the first=20 is Java and the 2nd is JavaScript running under Rhino.
 

public class Perf2 {
   =20
public=20 static=20 void = main(String[]=20 args) {
    String string =3D
"ABCDEFGHIJ";
    = String=20 sub;
   
int i =3D 1;
    =
long start =3D=20 System.currentTimeMillis();
    while=20 (i<200000)
    {
    =    =20 sub =3D string.substring(3,6);
        = i =3D i +=20 1;
    }
    long diff =3D=20 System.currentTimeMillis() - start;
   =20 System.out.println(
"Elapsed: = " + diff);
    = }   
}


function myFunc()
{
    var
string =3D=20 "ABCDEFGHIJ";
   =20 var sub;
    var = i;
    i=20 =3D 1;
   =20 while (i < 200000)
   =20 {
        sub =3D string.substring(3,=20 6);
        i =3D i + = 1;
   =20 }
}

myFunc();


The Java program takes 15 milliseconds, the = JavaScript takes=20 850 milliseconds.  I understand compiled Java will probably be = faster but I=20 am surprised at how much=20 faster.

------=_NextPart_000_0019_01C39437.A0589D60-- .