How fast is this?

Hi all,
I have a forloop which loops through an ArrayList full of Entities.

What the for loop does is that it loops through the arraylist, obtains the Entity from the arraylist, then does a Entity.Hashmap.get(arg_name); if its true, then break.

So here it is in code:


public void getEntity(Spatial s) {
  String name = s.getName();
  for (int i = 0; i < entityArrayList.size(); i++) {
    Entity t = (Entity) entityArrayList.get(i);
    boolean hasName = t.containsSpatialName(name);
    if (hasName == true) break;
  }
}

Thats all it does. The only problem is that its in the same thread as the renderer (and I dont whish to change that). The ArrayList will by large have around at max 5000 Entities and I was wondering how fast this is and how large of an FPS hit would I have to bear.

Thx, DP

Is it just me, or does that loop not do anything? ;D

Anyway, to answer your question: blindingly fast. Each operation will execute so fast you could blink and not see it happen :P.

Think about it: you’re asking us to tell you how fast a particular loop runs on any unknown PC. Perhaps you could instead ask something quantified and important like “realistically, can I call hashCode() more than 1 million times every 1 ms on a modern PC?”. Or something like “are there any performance problems with Arraylists with 5000 elements?” (to which the answer is YES!: be a little careful about add’s, and especially careful about indexOf’s and remove’s, which can be slow on mor than a few thousand elements)

OTOH, how many times do people have to say “optimize last” and “profile before optimizing”?

The question “this code is responsible for 65% of my runtime, which I can’t explain at all” then that would be a very good question for people to help with. As it stands, it’s a little hard to see how it could even show up at all in your profiler…

the above code was an extract, and is not intended to be full code. Here is the full code:


public Entity getEntity(Spatial s) { 
  String name = s.getName(); 
  for (int i = 0; i < entityArrayList.size(); i++) { 
    Entity t = (Entity) entityArrayList.get(i); 
    boolean hasName = t.containsSpatialName(name); 
    if (hasName == true) return t; 
  }
}

The reason I ask is because I cannot create a test case where that is the case, this is pure speculation and the possibility of it happening is there. If this will be the downfall of the FPS, then perhaps I should design in a different matter.

As for the arraylist comment, there will be no IndexOf calls, perhaps some remove (but that is rare).

I dont have a profiler because I dont have the money to buy either OptimiseIT or JProbe.

I should have said, “Is this design with speed on mind sound?” rather than “is this fast”

DP

Then I think the most important answer for you is “I don’t see anything stupid in there” (of course, I might have missed something ;)) - i.e. the actual code is not so spectacularly bad that it will fundamentally blow up (e.g. it’s not iterating over the values of a hashtable, which is definitely a bad thing to do).

As to profiling, java comes with a free profiler. As Cas has explained lots of times in the past (which is why I dislike forums for programming - his comments are now buried somewhere in the distant past of his 3000 posts!), that free profiler is more than sufficient to do 90% of your profiling work. It doesn’t draw fancy graphs, it doesn’t produce colourful output, and it doesn’t have a GUI. But…it does give you a very simple statement “these are the slowest parts of your program, this is how slow they are, and here are the line numbers” - which is all you need, really, unless you’re doing exceptional stuff.