The test intentionally creates very little garbage, just in the System.out I would assume.

I am running on a Mac, so of course it will be faster :)



On Apr 8, 2008, at 7:21 PM, Yonik Seeley wrote:

On Tue, Apr 8, 2008 at 7:48 PM, robert engels <[EMAIL PROTECTED]> wrote:
That is opposite of my testing:...

 The 'foreach' is consistently faster.

It's consistently slower for me (I tested java5 and java6 both with
-server on a P4).
I'm a big fan of testing different methods in different test runs
(because of hotspot, gc, etc).

Example results:
$ c:/opt/jdk16/bin/java -server t 100000000 10 foreach
N = 10
method=foreachlen=1000000000 indexed time = 8734

[EMAIL PROTECTED] /cygdrive/h/tmp
$ c:/opt/jdk16/bin/java -server t 100000000 10 iter
N = 10
method=iterlen=1000000000 indexed time = 7062


Here's my test code (a modified version of yours):

public class t {
   public static void main(String[] args) {
       int I = Integer.parseInt(args[0]); // 1000000
       int N = Integer.parseInt(args[1]); // 10
       String method = args[2].intern();  // foreach or iter

       String[] strings = new String[N];

       for (int i = 0; i < N; i++) {
           strings[i] = Integer.toString(i);
       }

       System.out.println("N = " + N);

       long len = 0;
       long start = System.currentTimeMillis();

       if (method=="foreach")
         for (int i = 0; i < I; i++) {
             for (String s : strings) {
                 len += s.length();
             }
         }
       else
         for (int i = 0; i < I; i++) {
             for (int j = 0; j < N; j++) {
                 len += strings[j].length();
             }
         }

           System.out.println("method="+method + "len="+len+" indexed
time = " + (System.currentTimeMillis() - start));
     }
}

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to