Add a benchmark for wrenCall().
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "benchmark.h"
|
||||
|
||||
@@ -14,9 +15,56 @@ static void arguments(WrenVM* vm)
|
||||
wrenSetSlotDouble(vm, 0, result);
|
||||
}
|
||||
|
||||
const char* testScript =
|
||||
"class Test {\n"
|
||||
" static method(a, b, c, d) { a + b + c + d }\n"
|
||||
"}\n";
|
||||
|
||||
static void call(WrenVM* vm)
|
||||
{
|
||||
int iterations = (int)wrenGetSlotDouble(vm, 1);
|
||||
|
||||
// Since the VM is not re-entrant, we can't call from within this foreign
|
||||
// method. Instead, make a new VM to run the call test in.
|
||||
WrenConfiguration config;
|
||||
wrenInitConfiguration(&config);
|
||||
WrenVM* otherVM = wrenNewVM(&config);
|
||||
|
||||
wrenInterpret(otherVM, testScript);
|
||||
|
||||
WrenValue* method = wrenGetMethod(otherVM, "main", "Test", "method(_,_,_,_)");
|
||||
|
||||
double startTime = (double)clock() / CLOCKS_PER_SEC;
|
||||
|
||||
double result = 0;
|
||||
for (int i = 0; i < iterations; i++)
|
||||
{
|
||||
WrenValue* resultValue;
|
||||
wrenCall(otherVM, method, &resultValue, "dddd", 1.0, 2.0, 3.0, 4.0);
|
||||
result += wrenGetValueDouble(otherVM, resultValue);
|
||||
wrenReleaseValue(otherVM, resultValue);
|
||||
}
|
||||
|
||||
double elapsed = (double)clock() / CLOCKS_PER_SEC - startTime;
|
||||
|
||||
wrenReleaseValue(otherVM, method);
|
||||
wrenFreeVM(otherVM);
|
||||
|
||||
if (result == (1.0 + 2.0 + 3.0 + 4.0) * iterations)
|
||||
{
|
||||
wrenSetSlotDouble(vm, 0, elapsed);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Got the wrong result.
|
||||
wrenSetSlotBool(vm, 0, false);
|
||||
}
|
||||
}
|
||||
|
||||
WrenForeignMethodFn benchmarkBindMethod(const char* signature)
|
||||
{
|
||||
if (strcmp(signature, "static Benchmark.arguments(_,_,_,_)") == 0) return arguments;
|
||||
if (strcmp(signature, "static Benchmark.call(_)") == 0) return call;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
+1
-1
@@ -64,7 +64,7 @@ static WrenForeignClassMethods bindForeignClass(
|
||||
}
|
||||
|
||||
static void afterLoad(WrenVM* vm) {
|
||||
if (strstr(testName, "call.wren") != NULL) callRunTests(vm);
|
||||
if (strstr(testName, "/call.wren") != NULL) callRunTests(vm);
|
||||
}
|
||||
|
||||
int main(int argc, const char* argv[])
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
class Benchmark {
|
||||
foreign static call(iterations)
|
||||
}
|
||||
|
||||
var result = Benchmark.call(1000000)
|
||||
// Returns false if it didn't calculate the right value. Otherwise returns the
|
||||
// elapsed time.
|
||||
System.print(result is Num)
|
||||
System.print("elapsed: %(result)")
|
||||
Reference in New Issue
Block a user