Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6937091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:19:10+00:00 2026-05-27T12:19:10+00:00

I am writing a simple 3D SW rendering engine. I have a default ArrayList<Object3D>

  • 0

I am writing a simple 3D SW rendering engine. I have a default ArrayList<Object3D> containing the whole scene. Now, I want to be able to add, remove and select objects by name, like 3D editors do (because its MUCH more simple than mouse select, but still looking good in homework 🙂 ).

So, the first thing I thought is to have Hashtable for name and index to scene ArrayList. But, then I thought I could just simply save the scene using Hashtable directly, and go through it to render using iterator.

So I want to ask, in a 3D engine, what is speed-preferable? Because I will for-loop the scene many times per second, compared to selecting object. Is ArrayList any faster than iterated Hashtable? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T12:19:11+00:00Added an answer on May 27, 2026 at 12:19 pm

    First, I suggest you use a HashMap instead of a Hashtable, for the same reason that ArrayList is a better choice than a Vector: less overhead due to useless synchronization.

    My guess is that iterating through an ArrayList will be faster than iterating through the Set returned by a Hashtable‘s (or HashMap‘s) entrySet() method. But the only way to know is to profile.

    Obviously, changes to the display list (other than appending or chopping off the last element) are going to be faster for a HashMap than for an ArrayList.

    EDIT
    So I followed my own advice and benchmarked. Here’s the code I used:

    import java.util.*;
    
    public class IterTest {
        static class Thing {
            Thing(String name) { this.name = name; }
            String name;
        }
    
        static class ArrayIterTest implements Runnable {
            private final ArrayList<Thing> list;
            ArrayIterTest(ArrayList<Thing> list) {
                this.list = list;
            }
            public void run() {
                int i = 0;
                for (Thing thing : list) {
                    ++i;
                }
            }
        }
    
        static class ArraySubscriptTest implements Runnable {
            private final ArrayList<Thing> list;
            ArraySubscriptTest(ArrayList<Thing> list) {
                this.list = list;
            }
            public void run() {
                int i = 0;
                int n = list.size();
                for (int j = 0; j < n; ++j) {
                    Thing thing = list.get(j);
                    ++i;
                }
            }
        }
    
        static class MapIterTest implements Runnable {
            private final Map<String, Thing> map;
            MapIterTest(Map<String, Thing> map) {
                this.map = map;
            }
            public void run() {
                int i = 0;
                Set<Map.Entry<String, Thing>> set = map.entrySet();
                for (Map.Entry<String, Thing> entry : set) {
                    ++i;
                }
            }
        }
    
        public static void main(String[] args) {
            final int ITERS = 10000;
            final Thing[] things = new Thing[1000];
            for (int i = 0; i < things.length; ++i) {
                things[i] = new Thing("thing " + i);
            }
            final ArrayList<Thing> arrayList = new ArrayList<Thing>();
            Collections.addAll(arrayList, things);
            final HashMap<String, Thing> hashMap = new HashMap<String, Thing>();
            for (Thing thing : things) {
                hashMap.put(thing.name, thing);
            }
            final ArrayIterTest t1 = new ArrayIterTest(arrayList);
            final ArraySubscriptTest t2 = new ArraySubscriptTest(arrayList);
            final MapIterTest t3 = new MapIterTest(hashMap);
            System.out.println("t1 time: " + time(t1, ITERS));
            System.out.println("t2 time: " + time(t2, ITERS));
            System.out.println("t3 time: " + time(t3, ITERS));
        }
    
        private static long time(Runnable runnable, int iters) {
            System.gc();
            long start = System.nanoTime();
            while (iters-- > 0) {
                runnable.run();
            }
            return System.nanoTime() - start;
        }
    }
    

    And here are the results for a typical run:

    t1 time: 41412897
    t2 time: 30580187
    t3 time: 146536728
    

    Clearly using an ArrayList is a big win (by a factor of 3-4) over a HashMap, at least for my style of iterating through a HashMap. I suspect the reason the array iterator is slower than array subscripting is all the iterator objects that need to be created and then garbage-collected.

    For reference, this was done with Java 1.6.0_26 (64-bit JVM) on an Intel 1.6GHz quad-core Windows machine with plenty of free memory.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a simple OpenGL application that uses GLUT . I don't want to
I'm writing a simple app that's going to have a tiny form sitting in
I am writing a simple database with web access. I have previous experience with
I am writing a small game engine for XNA. Right now my entity manager
I am writing a simple top down RPG in Pygame, and I have found
I am writing simple program for my Ubuntu 10.04. I want to see which
I'm writing a simple scene graph to hold some objects so I can control
I'm writing a simple JavaScript 3D engine from scratch using Canvas and box like
Writing simple parsers are trivial and I have implemented several over the years. In
I'm writing a simple chat with ajax and I have a problem with JSON.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.