Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Java micro questions.  (Read 525 times)
0 Members and 1 Guest are viewing this topic.
Offline lukasz1985

Senior Newbie





« Posted 2013-10-07 20:01:41 »

...

Offline Mac70
« Reply #1 - Posted 2013-10-07 20:06:38 »

"Premature optimization is the root of all evil".

Don't think about optimizing such things, JIT will do everything to make your code as fast as possible. Smiley

Check out my Devblog! Smiley
Offline sproingie

JGO Kernel


Medals: 202



« Reply #2 - Posted 2013-10-07 20:21:45 »

Java's JIT inlines things very aggressively -- it's quite likely the call overhead is zero because there is no call.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Mac70
« Reply #3 - Posted 2013-10-07 20:24:46 »

Don't think about performance overhead of things like multidimensional arrays, method calls (especially static method calls), the most efficient way to do anything is just to keep it simple and clean. Wink

Check out my Devblog! Smiley
Online Roquen
« Reply #4 - Posted 2013-10-07 21:55:29 »

HotSpot is very aggressive at inlining.  Additionally field accessors have a special cased optimization path.

@Mac70: I read this as a "how does it work" question.
Offline philfrei
« Reply #5 - Posted 2013-10-07 21:58:37 »

 Huh From B, you simply use "bar" directly.
1  
2  
3  
4  
5  
public class AAA
{
   protected int bar = 2;  
   protected int getBar() { return 2; }
}


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
public class BBB extends AAA
{
   public static void main(String[] args)
   {
      BBB bbb = new BBB();
      bbb.doSomething();
   }
   
   private void doSomething()
   {
      System.out.println(bar);
   }
}


To the best of my limited understanding, the following methodology is of mixed usefulness, but you can try putting the test cases in loops and executed them a million or so times, and check the elapsed time. I'd try to at least do something to have each iteration of the loop be slightly different. For example, set a variable to a different value each time:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
public void doSomethingTest()
{
    long startTime = System.nanoTime();
    for (int i = 0; i < 1000000; i++)
    {
        bbb.doSomething(i);  // put within doSomething: int foo = i;
   }
    long endTime = System.nanoTime();
    System.out.println("elapsed time: " + (endTime - startTime) );
}


I find it helps to run these multiple times, as each iteration will probably have a different result. Also, have to keep in mind that Java responds differently depending on when it is running from bytecode or memory, and the degree of caching can depend on whatever else is happening in the code. There is also no guarantee that elapsed times will be consistent across various OS / JVM combinations.

I don't know the actual answer, but I suspect, along with the others, that the differences you are asking about here are either non-existent or of extremely low relevance. However, there's been a number of times when I've done these sorts of tests and been surprised! So I wouldn't discourage taking the five or ten minutes needed to occasionally run a test like the above.

I'm trying to think about any resources that go into depth specifically about Java compilation and execution. The only one I know about is "Java Concurrency in Practice" (Goetz) which has a section on performance testing and a chapter on the Java Memory Model which gets into compiler details, but the book is pretty advanced (imho).

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Pages: [1]
  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 (35 views)
2014-07-24 01:59:36

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

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

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

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

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

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

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

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

Riven (56 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!