Bitfields vs. Polymorphism for game map object attributes
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)
{
开发者_如何学C if(List->MapObjectFlags & MAPOBJECTFLAG_RENDER)
OR
if(List->Render())
{
return List->Type;
}
else
{
List = List->Next;
}
}
bool XMapObject::Render()
{
return 1;
}
Thanks.
A bitwise operation is always faster than a virtual function call.
精彩评论