Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (480)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (546)
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  
  Storing a reference of an object vs using a 4 byte int ID  (Read 733 times)
0 Members and 1 Guest are viewing this topic.
Offline xslaught

Junior Newbie





« Posted 2012-01-04 08:42:45 »

Hello suppose I have an object say: Equip which contains a bunch of variables in the class.
I have already created the object and stored it into a Library object so it can be fetched later by an int ID.

Now suppose I create a Character object that wants to wear the Equip. The character object has a field say equipWeared.
I can either make equipWear an int to store the ID of the Equip I made or make it Equip type which is the class Equip is.
I am not making a new instance of Equip but just referencing it by assigning the variable the Equip from the Library object.

Which would be better and more efficient? Should I just put in an int ID number or put in a reference to the Equip?
Offline roland
« Reply #1 - Posted 2012-01-04 08:46:51 »

reference. Make the reference also have a method getID(); which returns the ID.
Otherwise each time you want to use this object you must get it from the library using the ID. This involves searching = slow. (I assume the library has a hashmap from Integer to Equip? Also It's just a reference, it's not a new object. Therefore it doesn't take much memory or anything if that's what you are worried about
By holding a reference, you only need to search for it once (when you get the Equip)
Offline theagentd
« Reply #2 - Posted 2012-01-04 13:12:52 »

There's no need to work around using references for performance reasons. References are guaranteed to be fast, while searching like Roland said will always be at best almost as fast. Even if your Equip instances are all in an array and you just write equipArray[equipWearID] to refer to it, I think references will be slightly faster. Besides, you also need to have a reference to a HashMap, some kind of List or an array containing the actual instances of Equips, right? This would make you need both an ID int AND a reference to the collection of Equips to pick from. So you're actually asking if one reference is slower than one reference and an ID.

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

JGO Kernel


Medals: 202



« Reply #3 - Posted 2012-01-04 16:34:36 »

This kind of micro-optimization is ludicrous.  Are you equipping millions of times a second?  No? Then don't worry about it.  Use what is simpler and easier to maintain, and that's going to be the direct reference (and in this case it happens to be faster).  Storing and following an indirect reference like an ID should be the job of a database persistence layer if you're using one, and not something you deal with by hand.

Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #4 - Posted 2012-01-05 18:35:21 »

Yeah I agree with sproingie, you are getting to a level of optimization that is absolutely ridiculous:
Quote
Premature optimizations are the root of all evil!

Now got make a game! Grin

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.

atombrot (20 views)
2014-08-19 09:29:53

Tekkerue (21 views)
2014-08-16 06:45:27

Tekkerue (21 views)
2014-08-16 06:22:17

Tekkerue (12 views)
2014-08-16 06:20:21

Tekkerue (19 views)
2014-08-16 06:12:11

Rayexar (55 views)
2014-08-11 02:49:23

BurntPizza (37 views)
2014-08-09 21:09:32

BurntPizza (27 views)
2014-08-08 02:01:56

Norakomi (35 views)
2014-08-06 19:49:38

BurntPizza (64 views)
2014-08-03 02:57:17
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!