Created
October 31, 2018 17:03
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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); | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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