Performance issue using for loop in script

Started by
10 comments, last by pjmendes 14 years ago
Hmm, your loops are accessing the script class members for each iteration. This may be the cause. Can you try rewriting the function like this:

    void update(float timeSinceLastFrame)   // update tile properties    {        uint totalTilesX = this.totalTilesX;        uint totalTilesY = this.totalTilesY;        for(uint idxColumn = 0; idxColumn < totalTilesX; ++idxColumn)        {            for(uint idxLine = 0; idxLine < totalTilesY; ++idxLine)            {            }        }        for(uint idxColumn = 0; idxColumn < totalTilesX; ++idxColumn)        {            for(uint idxLine = 0; idxLine < totalTilesY; ++idxLine)            {            }        }    }


This is likely going to give a great performance boost. Local variables are always the most efficient to access, as they are referencable directly by the op codes in the VM. Class members and global variables need sevaral steps to get to the final value.

I'll definitely look into optimizing the bytecode instructions to reduce the amount of steps needed for the VM to access the class members, but it will never be as quick as the local variables. So the above suggestion will always be valid.

Regards,
Andreas

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Advertisement
Indeed there is some improvement in referencing local variables following your suggestion (about 10% from a quick glance). However, since my tilemap implementation does a heavy use of accessing script class members inside several loops, the performance goes down too much for me to be able to implement this part in AngelScript, so I plan on moving it application-side.

I will keep this in mind when designing my engine, critical areas will not be done script-side, but I still wish to use the great power of AngelScripts (possibly for AI, each AI entity calling script callbacks).

Thanks for all the help, and for creating and improving and providing us with AngelScript!
Game Development Blog: http://pjmendes.blogspot.com/

This topic is closed to new replies.

Advertisement