Game Development Community

Script String Operation Efficiency

by Robert "Robc" Charney · in Torque Game Builder · 07/25/2006 (10:47 am) · 5 replies

I have used string information to classify objects because it was convenient when I started. But now I have reached the point where I need to improve frame rate performance and I'm searching for simple ways to make improvements. So should I consider changing things like this...

function goDoSomething()
{
   %object = new ScriptObject()
   {
      objectType = "unassigned"; 
   };
 
   %object.objectType = "warrior";

//or perhaps...

   %object.objectType = "otherDude";
   ...
then throughout the script I make inquiries in a manner such as...
if(%object.objectType $= "warrior")
{
   switch$ (%object.objectType)
   {
       case ("warrior"):
       case("otherDude"):
           ...
    }
}


Would I see any worthwhile improvement in performance if I changed to something more like this:

function someSetUpFunction()
{
   $warrior        = 1;
   $otherDude   = 2;
   ...
}

function goDoSomething()
{
   %object = new ScriptObject()
   {
      objectType = 0;
   }; 
   %object.objectType = $warrior;

//or perhaps...

   %object.objectType = $otherDude;
   ...
then throughout the script I make inquiries in a manner such as...
if(%object.objectType = $warrior)
{
   switch (%object.objectType)
   {
       case ($warrior):
           ...
    }
}

I am not a big fan of global variables so I don't want to introduce any more than necesary. Nevertheless, I do make a fair number of inquiries of string variables and I wondered if those of you with more experience than I could comment on these approaches. Is there significant overhead in using strings compared to using global variables as constants? Or would it require hundred's of thousands of operations for me to see any real difference in performance? I know that nested loops bring a few of my string operations up to a fairly high number.

#1
07/25/2006 (4:20 pm)
I've always used classes to reference types of object, and names when I want a specific object. Once you define a class for an object you can give it class-specific commands that automatically include the instance that calls the function (%this). For example:

function Warrior::goDoSomething(%this)
{
     %this.setLinearVelocityY(10);
}

As far as integer vars compared to string vars, I don't really know which one is faster, if any. Maybe one of the employees will know.

Hope this helps! :D
#2
07/26/2006 (12:55 pm)
Thanks Kevin, yes I use script classes extensively to organize my various entities, they help me avoid almost all global variables and keep most of my features fairly well encapsulated in class objects. I just left them out of my example to try and keep it as simple as possible and to try to zoom in on the question of whether string operations are inheritantly more costly than arithmetic operations.

My gut feeling is that string operations may be more costly but to such a small extent that it doesn't matter. Either that or I expect somebody to tell me I've asked a noob question and of course it should always be done one way or another... either way I will benefit from anyone's insight on the matter.
#3
07/26/2006 (8:09 pm)
Numeric operations are probably faster, but as you said, the effect is not likely significant unless you have a massive number of objects/functions going at once every frame. More likely is that the physics or rendering are causing the most problems. A good thing to do is profile and compare various tweaks.

Edit: Wanted to add that there is a secondary benefit from using globals as your type definitions. If you decide to change a type, you don't have to hunt down every place in your code where you used the string. Instead, the global assignment automagically changes them all for you when you change it.

I always try to isolate values that I know may change and assign them to a variable (typically all capitals to denote that it's a constant and global) so that later tweaks don't require hunting through gobs of code to change all of the places it's used.
#4
07/27/2006 (4:54 am)
Have a read up on code profiling, it's a much better way to determine areas of code that would really benefit from optimisation. Saves optimising what you think may be a bottleneck that really isn't. Torque has a nice built in profiler, there should be an article on TDN about how to use it along with forum posts.

As Jason states, using globals as constants is something I also do. I tend to place all my constants within a namespace such as

$GAME::DEATH_MSG = "You died hahahah!";
$GAME::SPAWN_MSG = "Run for your life!!!";

$GAME::ARENA::SIZE = "25 25";

etc

Whilst a player variable in the game namespace would be denoted by
$Game::Player = some object id;

I guess its just a matter of personal preference, you could just use a $CONST:: namespace and make a mental note not to change the values of anything in it, but I like the all uppercase = constant style, mixed case = a variable.
#5
07/27/2006 (9:35 am)
Its funny, I have glanced at the profiling parameters a number of times and left with the thought...

"That looks really useful, I need to roll up my sleeves and dive into it one of these days to see if there are any useful tools that I should be using."

Then I would get distracted by some issue that I needed to solve for the next version and forget completely about profiling. Looks look today is the day to tackle it! Thanks for the advice. I have used the debug banner to help me recognize object bloat and excessive collision calls. Needless to say my code got much better after I identified and eliminated collison calls for which I had no use. That was a huge savings. I have also optimized the number of objects that use physics just because it seemed the smart thing to do... I'm sure it would be useful to know exactly how much processer time is spent on these various activities.

I also need to put my global constants in their own namespace, that is good advice... thanks! I have seen the all caps style on many occasions and just never adopted it simply because so far I have been able to keep all of my globals as constant... the '$' is a flag to me that I am dealing with a constant. Nevertheless, it is such a popular style that I might adopt it to make my code more readable for others. I think I was first taught to use that style back in C++ classes awhile ago... I guess I am hearing that the style is still in favor?

Thanks again. I appreciate all the advice I can get!