views:

46

answers:

2

OGRE3D for example uses strings to identify objects, so each time the code does something on an object using its name (a string), it has to do string operations, and since a 3D engine is very sensitive on speed, how can it be a good way on doing it ?

When a computer has to do operations on a string, it does it sequentially, bytes after bytes, so it spend more CPU cycles if the string is longer...

Wouldn't it be faster to use plain variable names in code instead of using string identifiers ?

+1  A: 

Yes, it would be faster to use plain variable names in code instead of using string identifiers. But sometimes you don't know those names during build-time. Then you need an approach to handle names dynamically. An alternative approach could be to just use primitive integers instead of strings. The integer values could possibly be generated using a hash or a lookup table of strings. But using strings wouldn't violently slow down your program, unless you have a lot of strings of very huge length which all have the same starting characters and only have a few different characters at the end. Normally, comparing strings should lead to a false result relatively early and only for the "true match" it is necessary to compare the two strings completely. However, the comparison of some strings to find objects within a well-organized search structure (like trees etc.) is not the bottleneck of 3D engines and, thus, should not be part of optimization issues

Flinsch
A: 

you could just have a handle class that increments an integer every time you ask it for a handle:

HandleGenerator::HandleGenerator() : mHandle(0)
{
}

HandleGenerator::~HandleGenerator()
{
}

int HandleGenerator::getNewHandle()
{
    return mHandle++;
}

add a mutex to the getNewHandle function to make it thread safe:

int HandleGenerator::getNewHandle()
{
    MyMutexLocker locker(&mMutex);

    return mHandle++;
}
Tom