Skip to content

Instantly share code, notes, and snippets.

@jamesu
Created September 17, 2012 14:09
Show Gist options
  • Save jamesu/3737516 to your computer and use it in GitHub Desktop.
Save jamesu/3737516 to your computer and use it in GitHub Desktop.
TorqueScript: Function Parameters vs Variables
//
// TorqueScript: Function Parameters vs Variables
//
// This simple demonstration illustrates the speed difference in
// passing function parameters as variables vs using the string stack, in a simple function
// which adds two numbers together.
//
// scriptTest1 technically incurs a penalty of converting the input variables to strings, while
// scriptTest2 instead looks up and assigns global variables.
// scriptTest3 which re-assigns variables in the function call is included to gauge
// the cost of copying global to local variables.
// scriptTest4 and scriptTest5 are the native equivalents of functionWP(scriptTest1) and functionNP(scriptTest2).
//
// Observations:
// - scriptTest1 is the slowest script-only solution, though oddly enough in Torque3D DEBUG it is the fastest.
// - scriptTest2 is consistently the fastest script-only solution.
// - scriptTest3 in addition to looking up and copying global variables uses more instructions,
// so it's slower that scriptTest2.
// - scriptTest4 is consistently the fastest (despite the int->string conversion),
// perhaps because it requires the fewest script instructions, and it doesn't manipulate global variables.
// - scriptTest5 has to extract variables from the global variable list which can be fairly expensive
// depending on how many conflicts there are in the hash table. It also does an insert into the StringTable
// for each variable lookup.
//
// Conclusions:
// - TorqueScript is definitely not the fastest scripting language about.
// - Both function calls and variable lookups are expensive.
// - Torque3D's scripting implementation seems a bit faster.
// - The iPad is unbelievably slow.
// - It's best to only use TorqueScript for situations where performance isn't crucial.
//
function functionWP(%p1, %p2)
{
return %p1 + %p2;
}
function functionNP()
{
$_R = $_0 + $_1;
}
function functionRP()
{
%p1 = $_0;
%p2 = $_1;
$_R = %p1 + %p2;
}
function scriptTest1()
{
%start = getRealTime();
%count = 0;
for (%i=0; %i<1000000; %i++) {
%count += functionWP(999999, 10101010);
}
%end = getRealTime();
//echo("scriptTest1 result: " @ %count);
return %end - %start;
}
function scriptTest2()
{
%start = getRealTime();
%count = 0;
for (%i=0; %i<1000000; %i++) {
$_0 = 999999;
$_1 = 10101010;
functionNP();
%count += $_R;
}
%end = getRealTime();
//echo("scriptTest2 result: " @ %count);
return %end - %start;
}
function scriptTest3()
{
%start = getRealTime();
%count = 0;
for (%i=0; %i<1000000; %i++) {
$_0 = 999999;
$_1 = 10101010;
functionRP();
%count += $_R;
}
%end = getRealTime();
//echo("scriptTest3 result: " @ %count);
return %end - %start;
}
function scriptTest4()
{
%start = getRealTime();
%count = 0;
for (%i=0; %i<1000000; %i++) {
%count += functioCWP(999999, 10101010);
}
%end = getRealTime();
//echo("scriptTest4 result: " @ %count);
return %end - %start;
}
function scriptTest5()
{
%start = getRealTime();
%count = 0;
for (%i=0; %i<1000000; %i++) {
$_0 = 999999;
$_1 = 10101010;
functioCNP();
%count += $_R;
}
%end = getRealTime();
//echo("scriptTest5 result: " @ %count);
return %end - %start;
}
function runAndPrintTimesFor(%test)
{
%times = %test;
%count = 0;
for (%i=0; %i<5; %i++) {
eval("$time = " @ %test @ "();");
%times = %times @ "," @ $time;
%count += $time;
}
%mean = %count / 5.0;
echo(%times @ "," @ %mean);
}
function getTestTimes()
{
echo("Test,1,2,3,4,5,AVG");
runAndPrintTimesFor("scriptTest1");
runAndPrintTimesFor("scriptTest2");
runAndPrintTimesFor("scriptTest3");
runAndPrintTimesFor("scriptTest4");
runAndPrintTimesFor("scriptTest5");
echo("FIN");
}
// C++ Code for functioCWP & functioCNP
/*
ConsoleFunction(functioCWP, S32, 3, 3, "( fileName, outFilename )")
{
return dAtoi(argv[1]) + dAtoi(argv[2]);
}
ConsoleFunction(functioCNP, void, 1, 1, "( fileName, outFilename )")
{
S32 v1 = Con::getDirectIntVariable("$_0");
S32 v2 = Con::getDirectIntVariable("$_1");
Con::setIntVariable("$_R", v1 + v2);
}
// Extra functions for Console Code which go in Con and Dictionary
namespace Con
{
S32 getDirectIntVariable(const char *varName, S32 def)
{
const char *name = prependDollar(varName);
return gEvalState.globalVars.getIntVariable(StringTable->insert(name));
}
}
S32 Dictionary::getIntVariable(StringTableEntry name, bool *entValid)
{
Entry *ent = lookup(name);
if(ent)
{
if(entValid)
*entValid = true;
return ent->getIntValue();
}
if(entValid)
*entValid = false;
return 0;
}
*/
/*
Results:
(Note: Torque2D,iTorque2D use the same console code in this case. Torque3D is compiled for OSX)
Torque2D DEBUG (Macbook Pro 2010 2.4ghz)
Test,1,2,3,4,5,AVG
scriptTest1,5456,5344,5168,5168,5136,5254.4
scriptTest2,3520,3488,3488,3520,3456,3494.4
scriptTest3,6224,6224,6240,6192,6304,6236.8
scriptTest4,1504,1520,1472,1488,1504,1497.6
scriptTest5,2704,2704,2720,2688,2736,2710.4
FIN
Torque2D RELEASE (Macbook Pro 2010 2.4ghz)
Test,1,2,3,4,5,AVG
scriptTest1,4000,4064,3824,4176,3424,3897.6
scriptTest2,1872,1904,1856,1840,1872,1868.8
scriptTest3,3936,4096,3952,3920,3936,3968
scriptTest4,672,720,672,688,704,691.2
scriptTest5,1152,1184,1136,1312,1184,1193.6
iTorque2D DEBUG (iPad 3, -O0)
Test,1,2,3,4,5,AVG
scriptTest1,28992,29184,29312,29056,29184,29145.6
scriptTest2,18560,18560,18624,18304,18624,18534.4
scriptTest3,35392,35520,35520,35456,35776,35532.8
scriptTest4,7168,7296,7232,7296,7232,7244.8
scriptTest5,11712,11776,11712,11712,11776,11737.6
FIN
iTorque2D RELEASE (iPad 3 -O2)
Test,1,2,3,4,5,AVG
scriptTest1,21632,21632,21632,21696,21696,21657.6
scriptTest2,12480,12480,12480,12416,12480,12467.2
scriptTest3,25664,25856,25536,25536,25600,25638.4
scriptTest4,4992,5056,4992,4992,5056,5017.6
scriptTest5,7872,7808,7936,7936,7808,7872
FIN
Torque3D DEBUG (Macbook Pro 2010 2.4ghz)
Test,1,2,3,4,5,AVG
scriptTest1,4176,4096,4352,4016,4032,4134.4
scriptTest2,1840,1856,1808,1792,1920,1843.2
scriptTest3,4848,4656,4688,4720,4720,4726.4
scriptTest4,1120,1056,1248,1168,1104,1139.2
scriptTest5,2176,2080,2176,2112,2112,2131.2
FIN
Torque3D RELEASE (Macbook Pro 2010 2.4ghz)
Test,1,2,3,4,5,AVG
scriptTest1,3696,3504,2512,2208,2224,2828.8
scriptTest2,656,672,656,656,672,662.4
scriptTest3,2272,2352,2272,2288,2272,2291.2
scriptTest4,464,448,464,464,480,464
scriptTest5,944,960,960,944,960,953.6
FIN
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment