Java-Gaming.org Java4K winners: [ by our judges | by the community ]         
Featured games (67)
games approved by the League of Dukes
Games in Showcase (∞)
games submitted by our members



News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  Print  
  Efficiency Question?  (Read 643 times)
0 Members and 1 Guest are viewing this topic.
Offline Soljaragz

Jr. Member
**

Posts: 55



« on: 2006-02-08 19:42:58 »

If I have a grid and each position on grid is an ArrayList

is it better to

1. Construct all the ArrayLists at initialization, and just leave them active, even if their is nothing in there.

2. don't make any ArrayList at initialization, but when an object moves to that spot, that instantiate an ArrayList and add it to the list, but if that object moves to a different grid, then delete that ArrayList, and make a new ArrayList, at that Enemies position

I've already done the first, and it works fine, but just wondering if is faster to do option 2.
Offline Jeff

JGO Kernel
*****

Posts: 3535


Got any cats?


« Reply #1 on: 2006-02-09 04:43:12 »

If im understanding you, the only reason to do the latter is if you are trying to save memory.

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 princec
« League of Dukes »

JGO Kernel
*****

Posts: 8076
Medals: 91


Eh? Who? What? ... Me?


« Reply #2 on: 2006-02-09 06:37:57 »

If the grid is large then yes, construct them lazily.

Cas Smiley

Games published by our own members! Go get 'em!
Offline Jeff

JGO Kernel
*****

Posts: 3535


Got any cats?


« Reply #3 on: 2006-02-09 20:03:39 »

Oh was that  what he was asking?

I thought he was asking if he should dereference the lists when empty.


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 Soljaragz

Jr. Member
**

Posts: 55



« Reply #4 on: 2006-02-10 00:34:56 »

but isn't time consuming to recreate objects?

I read somewhere that instead of deleting an element in the list ( that you would need later, but not at the moment), its best to not delete it, but put it in a arrayList and pull it out of the arrayList when you need it.

im pretty sure that is faster than the first two,..................right?

Offline Riven
« League of Dukes »

JGO Kernel
*****

Posts: 5866
Medals: 255


Hand over your head.


« Reply #5 on: 2006-02-10 04:39:19 »

Most certainly even slower than reconstructing them.

Anyway... profile, then you don't have to ask, because then you *know* it.

Hi, appreciate more people! Σ ♥ = ¾

Learn how to award medals... and work your way up the social rankings
Offline cylab

JGO Kernel
*****

Posts: 1940
Medals: 27



« Reply #6 on: 2006-02-10 05:25:24 »

"Premature optimization is the root of all evil" [Tony Hoare]

Just optimize if you have a concrete performance-problem and do it after using a profiler ;-)

Mathias - I Know What [you] Did Last Summer!
Offline DzzD

JGO Kernel
*****

Posts: 2134
Medals: 16



« Reply #7 on: 2006-02-10 06:29:16 »

"Premature optimization is the root of all evil" [Tony Hoare]

Just optimize if you have a concrete performance-problem and do it after using a profiler ;-)

I totaly agree

Bruno

Offline Jeff

JGO Kernel
*****

Posts: 3535


Got any cats?


« Reply #8 on: 2006-02-10 20:48:37 »

Java allocation is lightening fast.  Collection of short lived objects is equally fast.

What you seem to be alluding to is "pooling".  On a modern (desktop) VM pooling almost never helps you, in fact it usually hurts you.  The exceptions are (a) If the object being allocated needs to allocate a system construct such as a Thread or a Socket (the system is slow to give these to you) OR you have so over complicated a constructor that YOU are making allocation slow.

It sound like you've been listening to either very old advice or advice from C/C++ programmers.

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
Pages: [1]
  Print  
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.091 seconds with 20 queries.