Skip to content

Instantly share code, notes, and snippets.

@jamesu
Created September 21, 2012 09:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesu/3760645 to your computer and use it in GitHub Desktop.
Save jamesu/3760645 to your computer and use it in GitHub Desktop.
TorqueScript Benchmarking Reference Code
#!/usr/bin/perl
use Time::HiRes qw ( time );
my $PR = 0;
my $P0 = 0;
my $P1 = 0;
print "ENTRY\n";
sub functionWP {
return $_[0] + $_[1];
}
sub functionNP {
$PR = $P0 + $P1;
}
sub functionRP {
my $p1 = $P0;
my $p2 = $P1;
$PR = $p1 + $p2;
}
sub scriptTest1 {
my $count = 0;
my $j;
for ($j=0; $j<1000000; $j++) {
$count += functionWP(999999, 10101010);
}
#print "Count: " . count;
}
sub scriptTest2 {
my $count = 0;
my $j;
for ($j=0; $j<1000000; $j++) {
$P0 = 999999;
$P1 = 10101010;
functionNP();
$count += $PR;
}
#print "Count: " . count;
}
sub scriptTest3 {
my $count = 0;
my $j;
for ($j=0; $j<1000000; $j++) {
$P0 = 999999;
$P1 = 10101010;
functionRP();
$count += $PR;
}
#print "Count: " . count;
}
sub scriptTest6 {
my $count = 0;
my $j;
for ($j=0; $j<1000000; $j++) {
my $value = 999999 + 10101010;
$count += $value;
}
#print "Count: " . count;
}
sub runTest {
$name = $_[0];
print $name;
my $count = 0;
for ($i=0; $i<5; $i++) {
my $start = time;
&$name();
my $test_time = time - $start;
$count += $test_time*1000;
print "," . ($test_time*1000);
}
print "," . ($count / 5.0) . "\n";
}
print "TEST,1,2,3,4,5,AVG\n";
runTest("scriptTest1");
runTest("scriptTest2");
runTest("scriptTest3");
runTest("scriptTest6");
#!/usr/bin/perl
use v6;
our $PR = 0;
our $P0 = 0;
our $P1 = 0;
print "ENTRY\n";
sub functionWP($p1, $p2) {
return $p1 + $p2;
}
sub functionNP() {
$PR = $P0 + $P1;
}
sub functionRP() {
my $p1 = $P0;
my $p2 = $P1;
$PR = $p1 + $p2;
}
sub scriptTest1() {
my $count = 0;
my $i;
loop ($i=0; $i<1000000; $i++) {
$count += functionWP(999999, 10101010);
}
#print "Count: " . count;
}
sub scriptTest2() {
my $count = 0;
my $i;
loop ($i=0; $i<1000000; $i++) {
$P0 = 999999;
$P1 = 10101010;
functionNP();
$count += $PR;
}
#print "Count: " . count;
}
sub scriptTest3() {
my $count = 0;
my $i;
loop ($i=0; $i<1000000; $i++) {
$P0 = 999999;
$P1 = 10101010;
functionRP();
$count += $PR;
}
#print "Count: " . count;
}
sub scriptTest6() {
my $count = 0;
my $i;
loop ($i=0; $i<1000000; $i++) {
my $value = 999999 + 10101010;
$count += $value;
}
#print "Count: " . count;
}
sub runTest($name, $block) {
print $name;
my $count = 0;
for 1..5 {
my $start = now;
$block();
my $test_time = now - $start;
$count += $test_time*1000;
print "," ~ ($test_time*1000);
}
print "," ~ ($count / 5.0) ~ "\n";
}
print "TEST,1,2,3,4,5,AVG\n";
runTest("scriptTest1", { scriptTest1() });
runTest("scriptTest2", { scriptTest2() });
runTest("scriptTest3", { scriptTest3() });
runTest("scriptTest6", { scriptTest6() });
// TorqueScript Benchmark
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 scriptTest6()
{
%start = getRealTime();
%count = 0;
for (%i=0; %i<1000000; %i++) {
%value = 999999 + 10101010;
%count += %value;
}
%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");
runAndPrintTimesFor("scriptTest6");
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
// Note you can use Con::getIntVariable() instead, but directly getting the int is faster
namespace Con
{
//S32 getDirectIntVariable(const char *varName, S32 def = 0);
S32 getDirectIntVariable(const char *varName, S32 def)
{
const char *name = prependDollar(varName);
return gEvalState.globalVars.getIntVariable(StringTable->insert(name));
}
}
// S32 getIntVariable(StringTableEntry name, bool *valid = NULL);
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;
}
*/
-- Lua Equivalent of TorqueScript Benchmark
PR = nil
P0 = nil
P1 = nil
function functionWP(p1, p2)
return p1 + p2
end
function functionNP()
PR = P0 + P1
end
function functionRP()
p1 = P0
p2 = P1
PR = p1 + p2
end
function scriptTest1()
start = os.clock()
count = 0
for i=1,1000000 do
count = count + functionWP(999999, 10101010)
end
--print "Count: " . count
return os.clock() - start
end
function scriptTest2()
start = os.clock()
count = 0
for i = 1,1000000 do
P0 = 999999
P1 = 10101010
functionNP()
count = count + PR
end
--print "Count: " . count
return os.clock() - start
end
function scriptTest3()
start = os.clock()
count = 0
for i=1,1000000 do
P0 = 999999
P1 = 10101010
functionRP()
count = count + PR
end
--print "Count: " . count
return os.clock() - start
end
function scriptTest6()
start = os.clock()
count = 0
for i=1,1000000 do
value = 999999 + 10101010
count = count + value
end
--print "Count: " . count
return os.clock() - start
end
gtime = 0
function runTest(testName)
io.write(testName)
local count = 0
for i=1,5 do
loadstring("gtime = " .. testName .. "()")()
count = count + (gtime*1000.0)
io.write(string.format(",%f", gtime*1000.0))
end
io.write(string.format(",%f\n", count / 5.0))
end
print("TEST,1,2,3,4,5,AVG")
runTest("scriptTest1")
runTest("scriptTest2")
runTest("scriptTest3")
runTest("scriptTest6")
# Ruby equivalent of TorqueScript Benchmark
require 'benchmark'
def functionWP(p1, p2)
p1 + p2
end
def functionNP()
$PR = $P0 + $P1
end
def functionRP()
p1 = $P0
p2 = $P1
$PR = p1 + p2
end
def scriptTest1()
count = 0
for i in 0...1000000
count += functionWP(999999, 10101010)
end
#puts "Count: #{count}"
end
def scriptTest2()
count = 0
for i in 0...1000000
$P0 = 999999
$P1 = 10101010
functionNP()
count += $PR
end
#puts "Count: #{count}"
end
def scriptTest3()
count = 0
for i in 0...1000000
$P0 = 999999
$P1 = 10101010
functionRP()
count += $PR
end
#puts "Count: #{count}"
end
def scriptTest6()
count = 0
for i in 0...1000000
value = 999999 + 10101010
count += value
end
#puts "Count: #{count}"
end
def runTest(name, &block)
print "#{name}"
count = 0
5.times do
start = Time.now
block.call
time = Time.now - start
count += time*1000
print ",#{time*1000}"
end
print ",#{count / 5.0}\n"
end
puts "TEST,1,2,3,4,5,AVG"
runTest("scriptTest1") { scriptTest1() }
runTest("scriptTest2") { scriptTest2() }
runTest("scriptTest3") { scriptTest3() }
runTest("scriptTest6") { scriptTest6() }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment