Compared to other embeddable scripting languages, TorqueScript on average is quite slow. Anyone who has used TorqueScript heavily in an iTorque project has probably realized this. Mobile devices are of course slow, but it seems to me even doing simple things in TorqueScript is costly.
After actually benchmarking a rather simple function call ( https://gist.github.com/3737516 ), I was shocked after seeing just how bad it can get.
I'm not quite sure what the best way of tackling this is, so I'll provide as much information as I can in this Issue.
My tests seem to indicate that function calls in TorqueScript appear to be quite expensive, whereas in other embeddable scripting languages this is far from the case. Variable lookups also appear to be costly though this is true of any scripting language.
For reference, I tested 6 methods of adding two numbers together:
- scriptTest1 returns p1 + p2
- scriptTest2 sets global variable $_R to $_0 + $_1
- scriptTest3 is the same as scriptTest2 except it copies the global to local variables (similar performance to test 1)
- scriptTest4 is the same as scriptTest1, except the addition is done in C (ends up being faster)
- scriptTest5 is the same as scriptTest2, except the addition is done in C (and is actually slower)
- scriptTest6 adds two literals with no function calls, and is the fastest
I've placed the specific code I used for benchmarking TorqueScript, Ruby and Lua in a gist: https://gist.github.com/3760645 . All results are in milliseconds, the last number being the average time taken.
Torquescript results (Macbook Pro 2010 2.4ghz):
Torque3D RELEASE
scriptTest1,2220,2196,2224,2248,2632,2304
scriptTest2,692,688,696,692,692,692
scriptTest3,2224,2208,2236,2220,2208,2219.2
scriptTest4,448,436,436,432,432,436.8
scriptTest5,964,956,960,960,976,963.2
scriptTest6,280,288,284,280,288,284
The worst case here is 2.3s in the pure script function call. It seems making a local variable is costly as test 3 is the second worst case. Test 5 is oddly slower than test 2, and would be even slower if I used the actual Con:: getIntVariable function which does an int->string->int conversion.
The best case is 0.29s, which beats mruby but not lua.
mruby results for tests 1,2,3,6 (latest version, Macbook Pro 2010 2.4ghz):
TEST,1,2,3,4,5,AVG
scriptTest1,642.965,616.527,607.443,604.453,607.673,615.8122
scriptTest2,717.744,717.211,716.863,726.13,714.424,718.4744
scriptTest3,729.728,726.318,722.327,728.388,722.21,725.7942
scriptTest6,490.863,481.995,495.214,484.136,481.721,486.7858
Note that the worst case here is 0.73s.
Lua results for tests 1,2,3,6 (v5.2.1, Macbook Pro 2010 2.4ghz):
TEST,1,2,3,4,5,AVG
scriptTest1,177.313000,173.866000,173.777000,173.635000,172.777000,174.273600
scriptTest2,365.673000,366.093000,366.950000,367.679000,373.810000,368.041000
scriptTest3,495.440000,493.399000,490.451000,492.778000,491.064000,492.626400
scriptTest6,151.421000,150.907000,150.767000,149.743000,151.732000,150.914000
Note that the worst case here is 0.49s.