• Advertisement

Archived

This topic is now archived and is closed to further replies.

[java] Using built-in vector / linked list vs. manual implementation

This topic is 5060 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I''m choosing between using the built in vector/linked list class from java (haven''t decided which) or makeing my own manually (ie an object with a next node) to handle my game objects. My gut feels that doing it manually should be faster, but is doing it manually worth the gain in speed? Especially for 5-15 objects on screen. And what about more objects (50-100)? which would be better then? Also, this is for a project, and doing it manually would impress my teacher more :-) but im lazy so if it''s not worth the trouble im not going to bother. Thanx..

Share this post


Link to post
Share on other sites
Advertisement
I''d suggest using what''s already there. I haven''t worked much with Java, but my guess is that they''ve done what they can to make it quite fast, and more importantly, bug free and standardized. This is certainly the case with C++. You''re unlikely to make it much faster, if at all, unless you know a lot of ways to take shortcuts, because you know about some specific properties of your data.

However, making your own can be good experience. But if you already have, then I would consider it more impressive to use the standard implementations that already exist.

Share this post


Link to post
Share on other sites
I don''t really think that it''ll make anything faster at all if you were to write your own (unless you do TONS of optimizations). After all, they wrote those utilities for a reason: So you don''t have to.

That said, it certainly can''t hurt to write your own that way you get proactice in doing so and you can understand better how they work. But that''s really the only reason I can think of to not use the builtin ones.

http://chaos.webhop.org

Share this post


Link to post
Share on other sites
For 5-10 I doubt it would matter what you use, and even 50-100 is not that taxing.

It would probably impress your teacher more to use the existing containers - unless the assignment is to write your own data structures.

Share this post


Link to post
Share on other sites
Well, you could get a very tiny speed improvement by coding your own data structures that didn''t require the casts to/from Object. But its usually not worth the effort, and for 50 - 100 elements, its definitely not worth the effort.

Use the built in containers

Share this post


Link to post
Share on other sites
Use ArrayList instead of Vector. It is a faster implementation. Unless you need code syncronization, ie multiple threads accessing the same data structure, you shouldn''t use Vector.



First make it work, then make it fast. --Brian Kernighan

The problems of this world cannot possibly be solved by skeptics or cynics whose horizons are limited by the obvious realities. We need men and women who can dream of things that never were. - John Fitzgerald Kennedy(35th US President)

Do not interrupt your enemy when he is making a mistake. - Napolean Bonaparte

Share this post


Link to post
Share on other sites
Doing it manually will be faster, for 2 reasons:

-no casting (like NuffSaid said)
-no synchronized methods (you mostly don''t need synchronized methods, but the methods of java.util.vector are synchronized)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I dont know where the conception came from, but object reference casting is not slow. All it has to do is check if the cast target is in the object''s inheritance hierarchy, it''s nothing like casting primitive data types.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
How can you possibly think that your own linked list would be faster? Please.

Share this post


Link to post
Share on other sites
What about stack, deque, or PATRICIA trie?
Then again why not just a simple array?

Share this post


Link to post
Share on other sites
quote:
but im lazy so if it's not worth the trouble im not going to bother.

Go with the laziness instinct if you want to succeed as a programmer. You should not be thinking about writing your own collections until you have your program up and running correctly with Sun's implementations AND you have performance problems AND you have used a profiler to show that the data structures are causing the problems.

Heed the advice of computer science pioneer Donald Knuth "Premature optimization is the root of all evil". It's harder than it sounds, the biggest mistake newbies do are to over-design and waste their time on places where it doesn't matter. You must learn to trust the laziness instinct in order to become a truly efficient programmer.

Btw, the people claiming that getting rid of casts will make your programs faster have probably failed to follow the steps above, as most people who have actually profiled programs running on Sun's later VMs will tell you that the VMs are excellent at optimizing away the costs of casts.

quote:
How can you possibly think that your own linked list would be faster? Please.

Linked lists are probably where he would have the best chance of achieving an improvement, because Sun's LinkedList implementation allocates a "list entry"-object for every object you store in the list. This extra allocation can be eliminated with your own structures, but should probably only be touched if it causes annoying garbage collection pauses as the allocation itself is quite fast in Sun's VMs.

[edited by - HenryAPe on April 18, 2004 4:09:00 PM]

Share this post


Link to post
Share on other sites

  • Advertisement