Process/Render loop Question

I’m wondering which would be faster, or if there is ANY difference one should expect in speed between the two (assuming a large-scale project with potentially hundreds of objects to sort through/process):


class ObjectManager {
 for(i = 0; i< numOfObjects; i++){
  processObject(i);
  renderObject(i):
 }
}

–Processing then immediately rendering each object in sequence…

or


class ObjectManager {
 for(i = 0; i < numOfObjects; i++){
  processObject(i);
 }
 for(i = 0; i < numOfObjects; i++) {
  renderObject(i):
}

–processing each object in sequence, then rendering them all straight through.

Also, is there ANY reason to do one over the other? Or is this the wholely wrong way to do it overall? Perhaps should the processing and rendering be seperated into different asynchronous threads? I’m not terribly familira with what sorts of things tend to work faster or not.

–Lareon

PS: Sorry if this is a repeat question, but a peripheral search through the archives presented me with nothing that looked like what I’m asking.

Simplest way to know is to benchmark your code. In some cases, having everything in a single loop is going to be faster due to effects like cache coherence. In other cases, two separate loops will be better due to cache coherence (or more correctly, having too much stuff in a single loop blows everything out of the cache all the time, so you have no coherence at all). Also contributing to how well each strategy works is bus bandwidth - memory, graphics card, I/O etc etc.

I’d suggest to process your objects first, and then render all of them at once. There are many benefits to this approach, including:

  • during the processing you may find out that you don’t even need to render some of the objects
  • you may need to redo your processing after collision detection phase
  • in general, it’s better to have graphics calls bunched together (and, if possible,render objects with similar attributes at once to avoid thrashing), as it helps primitives batching and thus improves performance
  • since you probably want to render your scene in one frame anyway, there’s really not much sense in splitting the rendering, since the copy from the back-buffer (or the flip) to the screen will happen at the end of your frame

[shameless plug]
Take a look at this article in JDJ (part one), which explains some basics of game development:
http://www.sys-con.com/story/?storyid=46663&DE=1
[/shameless plug]

There are two opposing cases to consider, and only a benchmark of your app will tell the difference…

If you are code cache limited (small objects & large functions) then running method 2 (two passes) is more efficient.

If you are data cache limited (using large objects, or multiple referenced objects for the updating and drawing) then a combined update & draw for each object is better.

You can guess which will be the case, but you’ll probably be wrong :stuck_out_tongue:

You probably won’t get any benefit by threading these as seperate tasks. Most times when people refer to a seperate thread for graphics, they are referring to the level of the underlying graphics engine: this needs to set up states & hand the correct data to the hardware, applying sorting by texture, renderstate etc. for optimal performance.

Cool, thanks, guys, for the information. I apprecited it!