views:

43

answers:

1

This optimzation question has been bugging me for the last day.

In my program (a simple roguelike game), I use bitwise flags to store the attributes of map objects, such as if they are solid, or if they are rendered. However, I could accomplish the thing using polymorphism to return the appropriate value.

My question is, is either way significantly faster or slower than the other way when used in a linked list loop? Also, is one better practice than the other?

An example of the code:

XMapObject *List = ListStart;

while(List != NULL)
{
    if(List->MapObjectFlags & MAPOBJECTFLAG_RENDER)
      OR
    if(List->Render())
    {
        return List->Type;
    }
    else
    {
        List = List->Next;
    }
}

bool XMapObject::Render()
{
     return 1;
}

Thanks.

+1  A: 

A bitwise operation is always faster than a virtual function call.

YeenFei
I thought so. After thinking about it too, its also more flexible.
Shawn B
How it can be more flexible, if number of bits in a single `int` (or whatever you use for storing) is quite limited?
n0rd
More flexible than my original example, which has the hard coded return.
Shawn B