开发者

Using strings to identify objects: what's the purpose?

开发者 https://www.devze.com 2023-01-21 15:19 出处:网络
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 sensitiv

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 pla开发者_运维百科in variable names in code instead of using string identifiers ?


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


Well I got the answer by a teacher:

In fact a string identifier, once in a map thus inserted in order, is quickly found with a dichotomic search.

0

精彩评论

暂无评论...
验证码 换一张
取 消