Stefan Karpinski's words (in https://groups.google.com/forum/#!searchin/julia-users/C$2B$2B$20call$20julia$20struct%7Csort:relevance/julia-users/KTMlJ15vzVA/2W3qOis7Kk8J) explained the results:
There's also the issue that we can and do turn Julia functions into > C-callable function pointers that can be invoked from C as if they were C > function pointers – this currently has zero overhead and if the Julia > function is fast, then calling it from C will also be fast. If these > require interpreter state, then that would need to be a function call > argument to every C-callable function, which is at odds with many C APIs > (although good libraries do allow for a void* data argument). Maybe this > could be made to work, but my suspicion is that it would introduce too much > overhead and destroy our current ability to do zero-cost two-way interop > with C. On Thursday, September 8, 2016 at 12:43:30 PM UTC+8, K leo wrote: > > I just did a test of calling a Julia function 100,000 times, both from > Julia and from C++. The execution times are very close. The results are > as follows. This is on Xubuntu 16.04 64bits. > > *********** Julia ********** > | | |_| | | | (_| | | Version 0.5.0-rc3+0 (2016-08-22 23:43 UTC) > _/ |\__'_|_|_|\__'_| | Official http://julialang.org/ release > |__/ | x86_64-unknown-linux-gnu > > julia> include("speedTest.jl") > speedTest > > julia> speedTest.TestLoop() > elapsed time: 3.21365718 seconds > 3.21365718 > > > *********** C++ *********** > > g++ -o test -fPIC -I$JULIA_DIR/include/julia test3.cpp -L$JULIA_DIR/lib/ > -L$JULIA_DIR/lib/julia -lLLVM-3.7.1 -ljulia > $JULIA_DIR/lib/julia/libstdc++.so.6 > > ./test > 3.22423 > > The codes are shown below: > Julai: > >> module speedTest >> >> >>> function TestFunc() >> >> f=0. >> >> for i=1:10000 >> >> f += Float64(i*fld(3,2))*sqrt(rand()+1.) >> >> end >> >> end >> >> >>> function TestLoop() >> >> tic() >> >> for i=1:100000 >> >> TestFunc() >> >> end >> >> toc() >> >> end >> >> >>> end >> >> > C++: > >> #include <julia.h> >> >> #include <iostream> >> >> #include <sys/time.h> >> >> using namespace std; >> >> typedef unsigned long long timestamp_t; >> >> >>> static timestamp_t get_timestamp () >> >> { >> >> struct timeval now; >> >> gettimeofday (&now, NULL); >> >> return now.tv_usec + (timestamp_t)now.tv_sec * 1000000; >> >> } >> >> >>> int main(int argc, char *argv[]) >> >> { >> >> jl_init(NULL); >> >> >>> jl_load("speedTest.jl"); >> >> jl_value_t * mod = (jl_value_t*)jl_eval_string("speedTest"); >> >> jl_function_t * func = jl_get_function((jl_module_t*)mod,"TestFunc"); >> >> >>> timestamp_t t0 = get_timestamp(); >> >> >>> for(int i=1; i<100000; i++) { >> >> jl_call0(func); >> >> } >> >> >>> timestamp_t t1 = get_timestamp(); >> >> >>> double secs = (t1 - t0) / 1000000.0L; >> >> cout<< secs << endl; >> >> >>> jl_atexit_hook(0); >> >> return 0; >> >> } >> >> >> > > > >