<html><body><div>Hello,<br><br>I am new to Racket. Currently I am evaluating it to find out if it works good enough for my work.<br><br>One of important aspects for me is efficiency of Foreign Function Interface.<br>Unfortunately, it seems that FFI is quite slow.<br><br>Please can you tell me if this is a bug in my code or FFI itself is slow? And if it is indeed slow, is there a way of improving it?<br><br>Here is the code I have:<br><br>-- test.c --<br><br>void do_test(void)<br>{}<br><br>-- test.rkt --<br><br>#lang racket/base<br><br>(require<br> ffi/unsafe<br> ffi/unsafe/define)<br><br>(define-ffi-definer define-t (ffi-lib "libtest"))<br><br>(define-t do_test (_fun -> _void))<br><br>(define (do_benchmark)<br> (time (for ([i (in-range 1000000)])<br> (do_test)))<br>)<br><br>(for ([i (in-range 10)])<br> (do_benchmark))<br><br>-- Makefile --<br><br>libtest.so: test.o<br> gcc -fPIC -shared -pthread -o libtest.so test.o<br><br>test.o: test.c<br> gcc test.c -fPIC -shared -pthread -c -O2 -o test.o<br><br>clean:<br> rm -f test.o libtest.so<br><br>---------<br>Running the test suggests that a call to "do_test" costs about 150 nanoseconds. I would expect something not larger than 5 nanoseconds. A test where C program calls this function shows the call costs 3 nanoseconds.<br><br>P.S. In real life I would want to pass data to the function, most likely in form of a C-structure.<br><br>P.P.S. While 150 nanoseconds may not seem like much, it easily becomes a problem when many of such calls need to be done.<br><br>With kind regards,<br>Denys Rtveliashvili<br data-mce-bogus="1"></div></body></html>