Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (532)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: 1 [2] 3
  ignore  |  Print  
  Are static non member methods faster?  (Read 13932 times)
0 Members and 1 Guest are viewing this topic.
Offline Jeff

JGO Coder




Got any cats?


« Reply #30 - Posted 2003-10-29 23:08:28 »

Oh, and here is Abies' test with the same fixes.  Same results.

I warned you guys, VM benchmarking is NOT simple.

Results:
1  
2  
3  
Instance method 9142ms
Static method 9274ms
Instance method 9153ms


Code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
public class AbiesTest {

public static final int SIZE = 100000000;

  public int iadd(int a, int b) {
    return a + b;
  }

  public static int sadd(int a, int b) {
    return a+b;
  }

  public void itest() {
    int v = 0;
    for ( int i =0; i < SIZE; i++ ) {
      int a = iadd(i,1);
      v = iadd(v,a);
      v = iadd(v,i);
      v = iadd(v,5);
      v = iadd(v,v);
      v = iadd(i,v);
    }
//System.out.println(v);
 }

  public void stest() {
    int v = 0;
    for ( int i =0; i < SIZE; i++ ) {
      int a = sadd(i,1);
      v = sadd(v,a);
      v = sadd(v,i);
      v = sadd(v,5);
      v = sadd(v,v);
      v = sadd(i,v);
    }
    //System.out.println(v);
 }



  public static void main(String[] argv) {
    Test t = new Test();
    t.itest();
    t.stest();
    long start = System.currentTimeMillis();
    for ( int i =0; i < 10; i++ ){
      t.itest();
    }
    System.out.println("Instance method " + (System.currentTimeMillis()-start) + "ms");
    start = System.currentTimeMillis();
    for ( int i =0; i < 10; i++ ){
      t.stest();
    }
    System.out.println("Static method " + (System.currentTimeMillis()-start) + "ms");
    start = System.currentTimeMillis();
    for ( int i =0; i < 10; i++ ){
      t.itest();
    }
    System.out.println("Instance method " + (System.currentTimeMillis()-start) + "ms");
  }
}

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline Jeff

JGO Coder




Got any cats?


« Reply #31 - Posted 2003-10-29 23:16:10 »

Now the problem with the original logic this whoel silliness was started by is this:

(1) Statement:  It takes more CPU to set up the non-static monomoprhic calss then static ones.

Answer: Not really true in hotspot.  All call sites are initially assumed to be monomorphic.  They become un-monomoprhic due to class loading (see below).

(2) Statement: it takes CPU to watch for it becomign non-monomorphic.

Answer: Again, not really true in Hotspot.  Hotspot detects a call site potentially becoming non mono-morphic by watching the class loads and seeing if a newly loaded class potentially overr-rides a method thats currently being called in a mon omorphic fashion.  There are pretty tricky data structures inside of HS to track all this.  

This is part of classloading for any class, and is a one-time cost per class load which makes it pretty insignificant at run-time.

(3) new Statement: static calls take less memory.
This MIGHT be true, but very much dependant on what your Vm does and how much it tries to optimize memory usage.
In practice i don't think it singificantly effects foot-print which is actually pretty dominated today by reflection information.

Can we put this to bed now?  The answer to the original question, in any modern VM, is an unambigous NO!


Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #32 - Posted 2003-10-30 03:58:28 »

Thanks Jeff, one more question, just 'cause I'm curious...
Quote
     b                              interpreter has been blocked until compile completes


I've never ever seen a line that doesn't have a 'b' (only tried client VM).  Is the interpreter always blocked when compiling?  I've tried on a dual CPU machine as well and I still always get the 'b'.  Maybe this is only ever different with the server VM?


Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jeff

JGO Coder




Got any cats?


« Reply #33 - Posted 2003-10-30 05:29:06 »

Dunno, I'll ask Mike-- my answer man on the VM team -- and get back to you Smiley

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline overnhet

Junior Member




Java games rock!


« Reply #34 - Posted 2003-10-30 06:06:08 »

Jeff : never heard of the -compile flag and it is not recognized by java and javac on my system

java -client Test :
Instance method 6430ms
Static method 2360ms
Instance method 6420ms

java -client AbiesTest :
Instance method 6310ms
Static method 2370ms
Instance method 6200ms

And I don't think it is silliness. IF there is a SIGNIFICANT difference, then it does make sense to use static methods rather than instance methods when it is possible.   Smiley
Offline abies

Senior Member





« Reply #35 - Posted 2003-10-30 06:25:11 »

As for the warmup - I have tried to do it, but making first two calls outside of measuring loop. It turned out to be not enough.

As for the commenting out printline, then it is not fair, because:
1) This way you make tested rountine no-op - very bad mistake with modern compilers
2) I do not agree that printing out to stdout will dominate benchmark - we are talking about 10 lines per few seconds, I'm not testing single ms differences
3) Printline is the same in both methods, so even with overhead, it should have same effect.

Even with your correction, I got:

Instance method 6594ms
Static method 5516ms
Instance method 6609ms
Static method 5500ms

For client Hotspot.

So as far as your (1) Statement, it is not true for client Hotspot for many computers. In some cases very wrong, in some cases slightly wrong, but always wrong.

As for the server jvm, at least my benchmark was showing some numbers (showing that both cases are same as fast), in your case I got only 0ms from top to down... indeed writing benchmark is a tricky business, this printout was there for a reason...

One more about printout overhead - you can change SIZE to 0 to check it, it is 0 ms (which means less than 10ms, which in turn means it does not affect this benchmark, as marigin of error is anyway around 50ms).

As for the statement (2), I agree - it is mostly one-time cost, only during classloading and even taking into account that classloading is a worst slowdown during java startup, I doubt if it makes any difference.

As for statement (3) I'm not sure if I have said something to this effect, if yes, I can take it back - it was not my point. I'm talking only about time performance.

It seems that client Hotspot is not 'any modern VM' Wink

Can we agree on statement, that static calls are faster in client jvm and do not make difference in server jvm ? It can be clearly seen from _everybody_ results except yours, so I think it affect enough people out there...

Artur Biesiadowski
Offline Jeff

JGO Coder




Got any cats?


« Reply #36 - Posted 2003-10-30 06:50:56 »

Quote
Jeff : never heard of the -compile flag and it is not recognized by java and javac on my system

java -client Test :
Instance method 6430ms
Static method 2360ms
Instance method 6420ms

java -client AbiesTest :
Instance method 6310ms
Static method 2370ms
Instance method 6200ms

And I don't think it is silliness. IF there is a SIGNIFICANT difference, then it does make sense to use static methods rather than instance methods when it is possible.   Smiley


sorry -Xcompile

it forces compilation of all methods immediately thereby removing some of the effect of the warmup.  But its best just
to properly warm the VM with a singiificantly long test run before you run your test.

I find it very odd that you are getting such divergent results from the same code I'm running.  What is your platform and VM?  I'll see if I can macth it in the lab.

In the end this is EXACTLY the problem with micro-benchmarks though.  As they do not behave like real code they are prone to getting hung up on, and over-reacting to, some inner complexity of the VM process.  Which is what I suspect is happenign to you here.

JK

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline Jeff

JGO Coder




Got any cats?


« Reply #37 - Posted 2003-10-30 06:58:23 »

Quote
Can we agree on statement, that static calls are faster in client jvm and do not make difference in server jvm ? It can be clearly seen from _everybody_ results except yours, so I think it affect enough people out there...


I don't follow your comment on the prints, maybe you could explain further.  The fzct of the matter is that a print (or any system io) within a test is effectively a sleep(random()) and will screw your results unless your test last so long that this is  in the noise (for "so long", figure hours).  If you explain better what you were trying to do maybe I can suggest a solution that wont mess up your readings.

In re client/server. On MacOSX I had no perceivable difference between -client and -server.  It would be good for whoever else had reported OSX numbers to check my results to see if they line up.  As I say, taken literally, my numbers show that static is (nominally) SLOWER on MacOSX.

Meanwhile I'll run both -client and -server on a Win2K box  in the lab tomorrow.  It wouldl help to get stats  on these test machines.  It would actually be a more prope and reliable test if, in addition, we broke it into two tests, one for each case, such that the ONLY difference between the situations is what we are trying to test.  When trying to really microbenchmark, control of variables is pretty critical.


Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline overnhet

Junior Member




Java games rock!


« Reply #38 - Posted 2003-10-30 07:07:22 »

JVM : Java Hotspot client VM ( 1.4.2-b28 )
OS : Win98 ver 4.10.2222
Hardware : P4 2.8C with 1024 Mo RAM

Quote
In the end this is EXACTLY the problem with micro-benchmarks though.  As they do not behave like real code they are prone to getting hung up on, and over-reacting to, some inner complexity of the VM process.  Which is what I suspect is happenign to you here.

Agreed, differences found here might not reflect reality. And chances are high that it is JVM/OS dependent which would explain why you got different results.
Offline abies

Senior Member





« Reply #39 - Posted 2003-10-30 07:16:19 »

Quote

I don't follow your comment on the prints, maybe you could explain further.  The fzct of the matter is that a print (or any system io) within a test is effectively a sleep(random()) and will screw your results unless your test last so long that this is  in the noise (for "so long", figure hours).  If you explain better what you were trying to do maybe I can suggest a solution that wont mess up your readings.


I agree that printout introduces random wait to benchmark. It indeed equals to sleep(random()) - but this random() is quite small. On normal machine it should be less than ms per printout. So we can add error +/- 1ms per printout to error of benchmark. With seconds-long benchmark, few printouts do not change the results, especially if you run benchmark few times.

Why printouts at all ? To make sure that result of method is actually needed. Without printout, 1.4.2 server on my home computer optimize this code to no-op, because it knows that all this computation is never used. With printout, Hotspot cannot 'cheat', because it HAS to print correct value to screen. I suppose that with enough levels of indirection, you can make Hotspot into believing that you need this value anyway - but there is no guarantee that you will succeed. On the other hand, printouts are fool-proof - only way to spoil them is precomputing result of entire function, which is hardly doable by jit with so long loop.

Artur Biesiadowski
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Jeff

JGO Coder




Got any cats?


« Reply #40 - Posted 2003-10-30 07:18:47 »

Quote
As for the warmup - I have tried to do it, but making first two calls outside of measuring loop. It turned out to be not enough.

As for the commenting out printline, then it is not fair, because:
1) This way you make tested rountine no-op - very bad mistake with modern compilers


Oh is THIS what you were trying to do.

Nice assumption.  Totally wrong.

Here are the results for a real noop test.

Results:
1  
2  
3  
Instance method 0ms
Static method 0ms
Instance method 0ms


Code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
package benchmarks;


public class AbiesNoopTest {

public static final int SIZE = 100000000;

  public int iadd(int a, int b) {
    return a + b;
  }

  public static int sadd(int a, int b) {
    return a+b;
  }

  public void itest() {
    /*
    int v = 0;
    for ( int i =0; i < SIZE; i++ ) {
      int a = iadd(i,1);
      v = iadd(v,a);
      v = iadd(v,i);
      v = iadd(v,5);
      v = iadd(v,v);
      v = iadd(i,v);
    }
    // return v;
//System.out.println(v);*/

  }

  public void stest() {
    /*
    int v = 0;
    for ( int i =0; i < SIZE; i++ ) {
      int a = sadd(i,1);
      v = sadd(v,a);
      v = sadd(v,i);
      v = sadd(v,5);
      v = sadd(v,v);
      v = sadd(i,v);
    }
    return v;
    //System.out.println(v);*/

  }



  public static void main(String[] argv) {
    AbiesNoopTest t = new AbiesNoopTest();
    int acc;
    t.itest();
    t.stest();
    long start = System.currentTimeMillis();
    for ( int i =0; i < 10; i++ ){
       t.itest();
    }
    System.out.println("Instance method " + (System.currentTimeMillis()-start) + "ms");
    start = System.currentTimeMillis();
    for ( int i =0; i < 10; i++ ){
      t.stest();
    }
    System.out.println("Static method " + (System.currentTimeMillis()-start) + "ms");
    start = System.currentTimeMillis();
    for ( int i =0; i < 10; i++ ){
      t.itest();
    }
    System.out.println("Instance method " + (System.currentTimeMillis()-start) + "ms");
  }
}


As you can see a REAL noop situation makes this execute almost immediately.  I knew this instinctively from all the benchmarks I've run in the past, but this proves the point.

In any event a print is the WRONG way to solve that.  If the compiler were truly smart enough to figure out that the loop did nothing (which would be quite a feat when you consider that its calling a sub function plus all the possabilities of side-effects) THEN what you should do is return the calculated value rather then void which would take the recognition of whether the value was used or not and mvoe it our of the scope of what you were testing.

NEVER NEVER NEVER do system IO in the midst of a test. I can't make that point strongly enough.



Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline Jeff

JGO Coder




Got any cats?


« Reply #41 - Posted 2003-10-30 07:21:09 »

Minor bug in "AbiesTest" which may be causing you to get values from a previous iteration (apologies).

Try this and see if you still get differing values on Windows...

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
package benchmarks;


public class AbiesTest {

public static final int SIZE = 100000000;

  public int iadd(int a, int b) {
    return a + b;
  }

  public static int sadd(int a, int b) {
    return a+b;
  }

  public void itest() {
    int v = 0;
    for ( int i =0; i < SIZE; i++ ) {
      int a = iadd(i,1);
      v = iadd(v,a);
      v = iadd(v,i);
      v = iadd(v,5);
      v = iadd(v,v);
      v = iadd(i,v);
    }
//System.out.println(v);
 }

  public void stest() {
    int v = 0;
    for ( int i =0; i < SIZE; i++ ) {
      int a = sadd(i,1);
      v = sadd(v,a);
      v = sadd(v,i);
      v = sadd(v,5);
      v = sadd(v,v);
      v = sadd(i,v);
    }
    //System.out.println(v);
 }



  public static void main(String[] argv) {
    AbiesTest t = new AbiesTest();
    t.itest();
    t.stest();
    long start = System.currentTimeMillis();
    for ( int i =0; i < 10; i++ ){
      t.itest();
    }
    System.out.println("Instance method " + (System.currentTimeMillis()-start) + "ms");
    start = System.currentTimeMillis();
    for ( int i =0; i < 10; i++ ){
      t.stest();
    }
    System.out.println("Static method " + (System.currentTimeMillis()-start) + "ms");
    start = System.currentTimeMillis();
    for ( int i =0; i < 10; i++ ){
      t.itest();
    }
    System.out.println("Instance method " + (System.currentTimeMillis()-start) + "ms");
  }
}


JK

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline Jeff

JGO Coder




Got any cats?


« Reply #42 - Posted 2003-10-30 07:25:22 »

Quote


I agree that printout introduces random wait to benchmark. It indeed equals to sleep(random()) - but this random() is quite small. On normal machine it should be less than ms per printout. .


Then Windows aint normal, my friend.  We had BIG problems with this in the JDK performance team in benchmarks til we figured it out.

(I should add that we saw exactly the same thing in CD-I which
was OS/9, no relation  to OSX.  I think you will find that printing in general takes MUCH longer then you think it does.)

In the end, its an uncontrollable variable, which is BAD juju when Microbenchmarking (or benchmarking period.)  

Rule #1: Make no assumptions
Rule #2: Test THOROUGHLY all the asusmptions you end up making anyway.

Remember, science is the act of proving yourself WRONG not right.  If, after exhaustive work, you fail to prove your assumptions incorrect, then you may have something.

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline Jeff

JGO Coder




Got any cats?


« Reply #43 - Posted 2003-10-30 07:52:03 »

So in the spirit of testing assumptions, I decided to see what time it takes to println on OSX.

I have a feeling that OSX, being a truely multi-tasking OS, doesnt block waiting for the print.  The end result is that you are right that on OSX  returning from a print takes under 1ms in the situation i happen to be running in.  But, as I said, its unpredictable since you are turning control over to the OS and I woudl hesitate to say that its under 1ms in all circumstances.

My past experiences with Windows NT was that it DOES block in the  kernel and prints take a whole lot longer even in the best case, but I'll check that out v. 2K in the lab tomorrow.


Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline overnhet

Junior Member




Java games rock!


« Reply #44 - Posted 2003-10-30 08:02:14 »

Quote
Minor bug in "AbiesTest" which may be causing you to get values from a previous iteration (apologies).

Lol ! I didn't noticed the mistake and I was wondering why I got same results with the two tests.  Grin

The new AbiesTest gives me :
Instance method 7080ms
Static method 3130ms
Instance method 7090ms

which is roughly equivalent to the previous one.

Also server JVM gives me 0ms too so abies is sort of right with that print thing : a statement that really uses the result is needed or the JVM may notice the operations are dumb and bypass them. Maybe we could return v, store it in an array and print this array at the end of the benchmark ? (shouldn't take that much time).
Offline Jeff

JGO Coder




Got any cats?


« Reply #45 - Posted 2003-10-30 08:18:10 »

Quote
Also server JVM gives me 0ms too so abies is sort of right with that print thing : a statement that really uses the result is needed or the JVM may notice the operations are dumb and bypass them.


Hmm. Then the optimizer is much smarter on Windows, which is odd since OSX's Vnm is a varient of Hotspot.

I'll give it a try on Win2k myself.  If that happoens then all that should be needed to goose it is to return the value, I doubt you even need oto store it though you might need to assign it

JK

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline Jeff

JGO Coder




Got any cats?


« Reply #46 - Posted 2003-10-30 08:23:54 »

Okay, this fixes it for Win32 while adding no random elements.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
package benchmarks;


public class AbiesTest {

public static final int SIZE = 100000000;

  public int iadd(int a, int b) {
    return a + b;
  }

  public static int sadd(int a, int b) {
    return a+b;
  }

  public int itest() {
    int v = 0;
    for ( int i =0; i < SIZE; i++ ) {
 int a = iadd(i,1);
 v = iadd(v,a);
 v = iadd(v,i);
 v = iadd(v,5);
 v = iadd(v,v);
 v = iadd(i,v);
    }
    return v;
//System.out.println(v);
 }

  public int stest() {
    int v = 0;
    for ( int i =0; i < SIZE; i++ ) {
 int a = sadd(i,1);
 v = sadd(v,a);
 v = sadd(v,i);
 v = sadd(v,5);
 v = sadd(v,v);
 v = sadd(i,v);
    }
    return v;
    //System.out.println(v);
 }



  public static void main(String[] argv) {
    AbiesTest t = new AbiesTest();
    t.itest();
    t.stest();
    long start = System.currentTimeMillis();
    for ( int i =0; i < 10; i++ ){
 t.itest();
    }
    System.out.println("Instance method " + (System.currentTimeMillis()-start) + "ms");
    start = System.currentTimeMillis();
    for ( int i =0; i < 10; i++ ){
 t.stest();
    }
    System.out.println("Static method " + (System.currentTimeMillis()-start) + "ms");
    start = System.currentTimeMillis();
    for ( int i =0; i < 10; i++ ){
 t.itest();
    }
    System.out.println("Instance method " + (System.currentTimeMillis()-start) + "ms");
  }
}

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline Jeff

JGO Coder




Got any cats?


« Reply #47 - Posted 2003-10-30 08:34:25 »

Okay, the proper code as above,  on my Win2K box, jdk 1.4.1

Run in both cases with -Xcompile to "warm up" the VM.

(AMD Athalon 1.2)

Server results:
1  
2  
3  
4  
5  
Instance method 4126ms

Static method 4146ms

Instance method 4206ms


Client Results:
1  
2  
3  
4  
5  
Instance method 25757ms

Static method 6018ms

Instance method 25958ms


SO I can concurr that in Win32 Hotspot jdk1.4.1 in the server case there is no difference, but in the client case there is a significant one. (about a factor of 4)
This is NOT true under OSX.

This looks an awful lot like a client-compiler bug, but its *possible* that on Windows the client compiler is sacrificing agressive inling deliberately.  I'll need to go talk to the VM guys.

Yes, to be honest, if this were deliberate then I'd have to say that the client compiler doesn't meet my definition of a "modern jit".

Just out of curiosity, has anyoen tried this on IBM's latest?  maybe I will...




Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline overnhet

Junior Member




Java games rock!


« Reply #48 - Posted 2003-10-30 08:38:26 »

I did almost the same changes, except that I store v values in an int[10] and print them (outside the time measuring block).

With server JVM I got :
Instance method 770ms
Static method 830ms
Instance method 770ms

Definitely JVM/OS dependent. Well, unless there are evidences that some JVMs behave better with instance methods, I would say : use what fits best in terms of design, and if you have to choose between static and instance (eg : singleton vs static class discussion) go with static.

Could someone post results obtained with other JVMs/OSes, so that we have much more material ?
Offline Jeff

JGO Coder




Got any cats?


« Reply #49 - Posted 2003-10-30 08:42:56 »

The other possability is that there is a -XX flag for agressive inling in the client VM. I'll look into that too...

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline Jeff

JGO Coder




Got any cats?


« Reply #50 - Posted 2003-10-30 08:46:41 »

I'm DLing Sun 1.4.2 right now to test against.

Does anyoen know a way to get IBM's Win32 1.3 WITHOUT donwloading the whole damn websphere development kit???

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline Jeff

JGO Coder




Got any cats?


« Reply #51 - Posted 2003-10-30 08:59:20 »

Client numbers for 1.4.2_02.

Less drastic by far, but still significant:
1  
2  
3  
Instance method 12137ms
Static method 8843ms
Instance method 12127ms


I definitely need to ask the VM guys whats going on here.  It looks kinda like some 'work in progress' under the hood of the client VM...

Just for completeness, here is server.  No suprises here.
Its worth noting though that server in either config (which are the same) are half the best nukbers of client.

So it loosk to me like, if you are concerned about speed of calls, you probably want to avoid the client VM ANYWAY...

1  
2  
3  
Instance method 4026ms
Static method 4026ms
Instance method 4016ms


Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline Jeff

JGO Coder




Got any cats?


« Reply #52 - Posted 2003-10-30 09:04:59 »

Intresting link here for anyone interested in performance:

http://java.sun.com/j2se/1.4.2/1.4.2_whitepaper.html

I'm perusing it now for any clues to this client compierl behavior.

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline Jeff

JGO Coder




Got any cats?


« Reply #53 - Posted 2003-10-30 09:10:27 »

Hmm.  This would suggest that inlining is suppsoed to be happening...

"The final phase does peephole optimization on the LIR and generates machine code from it. Emphasis is placed on extracting and preserving as much information as possible from the bytecodes. It focuses on local code quality and does very few global optimizations, since those are often the most expensive in terms of compile time. It supports inlining any function that has no exception handlers or synchronization, and also supports deoptimization for debugging and inlining. "

http://java.sun.com/products/hotspot/docs/whitepaper/Java_Hotspot_v1.4.1/Java_HSpot_WP_v1.4.1_1002_4.html#client


Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline Jeff

JGO Coder




Got any cats?


« Reply #54 - Posted 2003-10-30 09:42:18 »

JUST to confuse things further....
I was a bit disturbed by the two levels of calls in the test as given because it compliacates the call chain.  Again, in the interests of reducing variables, I simplified it to direct calls to the static and non-static functions.

For this test I still kept Abies multiple operations per loop though I'm a bit concerned about that complication (I'll factor that out next set of tests.)

I also increased the numebr of tiems we run each test, just as a sanity check.

The results were interesting though not too out of the ordinary.  I had the labels backwarsd (Instacne is static and static is instance) so don't let that throw you.

The results:

Client:
1  
2  
3  
4  
5  
6  
7  
java -Xcompile -client benchmarks.SimplifiedTest
Instance method (-705032721) 9734ms
Static method (-705032721) 11136ms
Instance method (-705032721) 9714ms
Static method (-705032721) 11136ms
Instance method (-705032721) 9704ms
Static method (-705032721) 11146ms


Server:
1  
2  
3  
4  
5  
6  
7  
java -Xcompile -server benchmarks.SimplifiedTest
Instance method (-705032721) 4136ms
Static method (-705032721) 4076ms
Instance method (-705032721) 4096ms
Static method (-705032721) 4356ms
Instance method (-705032721) 4036ms
Static method (-705032721) 4347ms


here's the test:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
package benchmarks;


public class SimplifiedTest {

public static final int SIZE = 100000000;

  public int iadd(int a, int b) {
    return a + b;
  }

  public static int sadd(int a, int b) {
    return a+b;
  }




  public static void main(String[] argv) {
    SimplifiedTest t = new SimplifiedTest();
    for (int q = 0; q < 3; q++) {
      int v = 0;
      long start = System.currentTimeMillis();
      for (int i = 0; i < SIZE * 10; i++) {
        int a = t.iadd(i, 1);
        v = t.sadd(v, a);
        v = t.sadd(v, i);
        v = t.sadd(v, 5);
        v = t.sadd(v, v);
        v = t.sadd(i, v);
      }
      System.out.println("Instance method (" + v + ") " +
                         (System.currentTimeMillis() - start) + "ms");
      v = 0;
      start = System.currentTimeMillis();
      for (int i = 0; i < SIZE * 10; i++) {
        int a = t.iadd(i, 1);
        v = t.iadd(v, a);
        v = t.iadd(v, i);
        v = t.iadd(v, 5);
        v = t.iadd(v, v);
        v = t.iadd(i, v);
      }
      System.out.println("Static method (" + v + ") " +
                         (System.currentTimeMillis() - start) + "ms");
    }
  }
}


Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline abies

Senior Member





« Reply #55 - Posted 2003-10-30 09:49:24 »

1.4.1_01, Athlon/win2000

Client:
Instance method 17875ms
Static method 4235ms
Instance method 17828ms

Server:
Instance method 2906ms
Static method 2906ms
Instance method 2906ms

Ibm jre 1.4.0:
Instance method 3516ms
Static method 3875ms
Instance method 3531ms

Well... yes... what can I say...

Interesting thing - if I add printouts to test, static test in ibm jre has same speed as instance test - 3890ms. Strange, very strange - I really doubt if printout wait is causing this, I rather suspect that most of these 10-20% differences are from different pairing of instructions generated rather the real difference in jit quality. I have removed printouts and added 3 ?add(i,v) instructions in both methods and results are the same (6300ms).

At the moment it seems to me that for ibm and Hotspot server, speed is the same, with any umpteen percent differences in any direction caused by random thing like pairing of instruction or cache line boundary. So I give up as far as trying to prove that static method is _generally_ faster. I was proven wrong and I'm just left with statement that Client hotspot cannot manage to inline instance functions in reasonable way.

Artur Biesiadowski
Offline princec

JGO Kernel


Medals: 342
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #56 - Posted 2003-10-30 10:03:31 »

Tried -XX:CompileThreshold=<some bigger number>?

Still waiting for that 2 stage compiling VM Wink Our games depend on it!

Cas Smiley

Offline Jeff

JGO Coder




Got any cats?


« Reply #57 - Posted 2003-10-30 10:18:32 »

Okay,

I simplified it one step further and now Im getting BIZZARE results from the server compiler.

I definitely need to go talk to the VM guys. It seems very situation-dependant on what you get for instance v. static calls....

In an over-simplifed example now im getting a MASSIVE difference between the two for the server VM. But I'm not sure how real it is yet.


Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline abies

Senior Member





« Reply #58 - Posted 2003-10-30 10:23:01 »

Be sure to use SimplifiedTest.sadd instead of t.sadd - t.sadd is not very 'proper' way and generated few extra bytecodes. And first iadd should be sadd.

SimplifiedTest on ibm jre still gives better results to instance method... (I have corrected printed names, so I'm sure it is it)

I think that I have found a bug in server jvm:

Static method (-1.0E8) 2578ms
Instance method (-1.0E8) 5968ms
Static method (-1.0E8) 6016ms
Instance method (-1.0E8) 5875ms
Static method (-1.0E8) 5969ms
Instance method (-1.0E8) 5890ms

This is after changing int to float, dividing number of iterations by 10 and changing last two lines to
v = t.iadd(v, -v);
v = t.iadd(-i, v);
to avoid Infinity values.

It seems that server Hotspot make same kind of wrong optimalization given enough time.

Artur Biesiadowski
Offline overnhet

Junior Member




Java games rock!


« Reply #59 - Posted 2003-10-30 10:44:43 »

Just ran SimplifiedTest with message correction Wink

* client :
Static method (-705032721) 3960ms
Instance method (-705032721) 7250ms
Static method (-705032721) 3790ms
Instance method (-705032721) 7190ms
Static method (-705032721) 3850ms
Instance method (-705032721) 7250ms

* server :
Static method (-705032721) 830ms
Instance method (-705032721) 2470ms
Static method (-705032721) 1760ms
Instance method (-705032721) 2360ms
Static method (-705032721) 1540ms
Instance method (-705032721) 2410ms
Pages: 1 [2] 3
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (17 views)
2014-07-24 01:59:36

Riven (17 views)
2014-07-23 21:16:32

Riven (14 views)
2014-07-23 21:07:15

Riven (17 views)
2014-07-23 20:56:16

ctomni231 (45 views)
2014-07-18 06:55:21

Zero Volt (40 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (36 views)
2014-07-16 23:30:00

Cero (51 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!