Skip to content

Instantly share code, notes, and snippets.

@facontidavide
Last active February 11, 2020 08:29
Show Gist options
  • Save facontidavide/e9fdfa1e929fb61142c6d3300f2512c6 to your computer and use it in GitHub Desktop.
Save facontidavide/e9fdfa1e929fb61142c6d3300f2512c6 to your computer and use it in GitHub Desktop.
Lua vs QT JS
#include <iostream>
#include <QJSEngine>
#include "sol.hpp" //https://github.com/ThePhD/sol2
#include <benchmark/benchmark.h>
static void QmlFunction(benchmark::State& state) {
QJSEngine js;
js.evaluate("function calc(time, value){ return value*2 }");
QJSValue func = js.evaluate("calc");
for (auto _ : state) {
QJSValue result = func.call({QJSValue(1), QJSValue(2)});
benchmark::DoNotOptimize(result.toNumber());
}
}
BENCHMARK(QmlFunction);
static void LuaFunction(benchmark::State& state) {
sol::state lua;
lua.open_libraries();
lua.script("function calc(time, value) return value*2 end");
sol::function func = lua["calc"];
for (auto _ : state) {
sol::function_result result = func(1, 2);
benchmark::DoNotOptimize(result.get<double>(0));
}
}
BENCHMARK(LuaFunction);
/*
* ------------------------------------------------------
Benchmark Time CPU Iterations
------------------------------------------------------
QmlFunction 498 ns 498 ns 1379603
LuaFunction 74.2 ns 74.2 ns 9476946
*/
BENCHMARK_MAIN();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment