Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (780)
Games in Android Showcase (233)
games submitted by our members
Games in WIP (857)
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 1785 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.

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: 508
Projects: 3
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:
Premature optimizations are the root of all evil!

Now got make a game! Grin

Pages: [1]
  ignore  |  Print  

hadezbladez (745 views)
2018-11-16 13:46:03

hadezbladez (383 views)
2018-11-16 13:41:33

hadezbladez (735 views)
2018-11-16 13:35:35

hadezbladez (194 views)
2018-11-16 13:32:03

EgonOlsen (2393 views)
2018-06-10 19:43:48

EgonOlsen (2556 views)
2018-06-10 19:43:44

EgonOlsen (1482 views)
2018-06-10 19:43:20

DesertCoockie (2145 views)
2018-05-13 18:23:11

nelsongames (1949 views)
2018-04-24 18:15:36

nelsongames (2629 views)
2018-04-24 18:14:32
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46 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‑
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!