Skip to content

Instantly share code, notes, and snippets.

@kim366
Created October 31, 2018 17:03
Show Gist options
  • Select an option

  • Save kim366/13597bb20146c821b9dc03fc99ca5df1 to your computer and use it in GitHub Desktop.

Select an option

Save kim366/13597bb20146c821b9dc03fc99ca5df1 to your computer and use it in GitHub Desktop.
Julia Benchmark comparing the performance of `jl_eval_string` over `jl_get_global` and evaluating every frame. 648x (!) speed increase
#include "/usr/include/julia/julia.h"
#include <chrono>
#include <cstdio>
int main() {
jl_init();
std::chrono::high_resolution_clock c;
jl_eval_string("module Some module Nested module Module function f(a::Int64) "
"end end end end");
auto t1 = c.now();
for (int i = 0; i < 100000; ++i)
jl_call1(jl_eval_string("Some.Nested.Module.f"), jl_box_int64(5));
auto t2 = c.now();
jl_module_t *some, *nested, *module;
jl_function_t *f;
JL_GC_PUSH4(some, nested, module, f);
some = reinterpret_cast<jl_module_t *>(
jl_get_global(jl_main_module, jl_symbol("Some")));
nested =
reinterpret_cast<jl_module_t *>(jl_get_global(some, jl_symbol("Nested")));
module = reinterpret_cast<jl_module_t *>(
jl_get_global(nested, jl_symbol("Module")));
f = jl_get_function(module, "f");
for (int i = 0; i < 100000; ++i)
jl_call1(f, jl_box_int64(5));
JL_GC_POP();
auto t3 = c.now();
long d1 = std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1)
.count(),
d2 = std::chrono::duration_cast<std::chrono::microseconds>(t3 - t2)
.count();
printf("%ldms, %ldms, %Lfx speed increase\n", d1, d2, (long double)d1 / d2);
jl_atexit_hook(0);
}
@kim366
Copy link
Author

kim366 commented Oct 31, 2018

Compile with g++ -g -o eval -I/usr/include/julia -DJULIA_ENABLE_THREADING=1 -fPIC -L'/usr/lib' -Wl,--export-dynamic -Wl,-rpath,'/usr/lib' -Wl,-rpath,'/usr/lib/julia' juliaeval.cpp -ljulia

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment