Created
April 5, 2014 12:31
-
-
Save MgaMPKAy/9991396 to your computer and use it in GitHub Desktop.
Safe and unsafe FFI slowdown compare to C
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 <whitedb/dbapi.h> | |
int main(int argc, char **argv) { | |
void *db, *rec; | |
wg_int enc; | |
db = wg_attach_database("233", 1000000000); | |
char *str = "Hello, world"; | |
for (int i = 0; i < 10000000; i++) { | |
rec = wg_create_record(db, 1); | |
enc = wg_encode_str(db, str, NULL); | |
wg_set_field(db, rec, 0, enc); | |
} | |
wg_detach_database(db); | |
return 0; | |
} |
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
/usr/bin/time -v ./c | |
Command being timed: "./c" | |
User time (seconds): 1.16 | |
System time (seconds): 0.63 | |
Percent of CPU this job got: 99% | |
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:01.80 | |
Average shared text size (kbytes): 0 | |
Average unshared data size (kbytes): 0 | |
Average stack size (kbytes): 0 | |
Average total size (kbytes): 0 | |
Maximum resident set size (kbytes): 725920 | |
Average resident set size (kbytes): 0 | |
Major (requiring I/O) page faults: 0 | |
Minor (reclaiming a frame) page faults: 181535 | |
Voluntary context switches: 1 | |
Involuntary context switches: 186 | |
Swaps: 0 | |
File system inputs: 0 | |
File system outputs: 0 | |
Socket messages sent: 0 | |
Socket messages received: 0 | |
Signals delivered: 0 | |
Page size (bytes): 4096 | |
Exit status: 0 | |
/usr/bin/time -v ./UnsafeFFI | |
Command being timed: "./UnsafeFFI" | |
User time (seconds): 1.24 | |
System time (seconds): 0.63 | |
Percent of CPU this job got: 99% | |
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:01.89 | |
Average shared text size (kbytes): 0 | |
Average unshared data size (kbytes): 0 | |
Average stack size (kbytes): 0 | |
Average total size (kbytes): 0 | |
Maximum resident set size (kbytes): 726704 | |
Average resident set size (kbytes): 0 | |
Major (requiring I/O) page faults: 0 | |
Minor (reclaiming a frame) page faults: 181801 | |
Voluntary context switches: 1 | |
Involuntary context switches: 196 | |
Swaps: 0 | |
File system inputs: 0 | |
File system outputs: 0 | |
Socket messages sent: 0 | |
Socket messages received: 0 | |
Signals delivered: 0 | |
Page size (bytes): 4096 | |
Exit status: 0 | |
/tmp/tmproot/bin/time -v ./SafeFFI | |
Command being timed: "./SafeFFI" | |
User time (seconds): 3.42 | |
System time (seconds): 0.70 | |
Percent of CPU this job got: 99% | |
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:04.13 | |
Average shared text size (kbytes): 0 | |
Average unshared data size (kbytes): 0 | |
Average stack size (kbytes): 0 | |
Average total size (kbytes): 0 | |
Maximum resident set size (kbytes): 726448 | |
Average resident set size (kbytes): 0 | |
Major (requiring I/O) page faults: 0 | |
Minor (reclaiming a frame) page faults: 181796 | |
Voluntary context switches: 1 | |
Involuntary context switches: 429 | |
Swaps: 0 | |
File system inputs: 0 | |
File system outputs: 0 | |
Socket messages sent: 0 | |
Socket messages received: 0 | |
Signals delivered: 0 | |
Page size (bytes): 4096 | |
Exit status: 0 |
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
{-# LANGUAGE ForeignFunctionInterface #-} | |
import Foreign | |
import Foreign.C | |
data WhiteDB | |
data Record | |
foreign import ccall "wg_attach_database" c_wg_attach_database :: CString -> CInt -> IO (Ptr WhiteDB) | |
foreign import ccall "wg_detach_database" c_wg_detach_database :: Ptr WhiteDB -> IO CInt | |
foreign import ccall "wg_create_record" c_wg_create_record :: Ptr WhiteDB -> CInt -> IO (Ptr Record) | |
foreign import ccall "wg_set_field" c_wg_set_field :: Ptr WhiteDB -> Ptr Record -> CInt -> CInt -> IO CInt | |
foreign import ccall "wg_int wg_encode_str" c_wg_encode_str :: Ptr WhiteDB -> CString -> CInt -> IO CInt | |
foreign import ccall "wg_int wg_decode_str" c_wg_decode_str :: Ptr WhiteDB -> CInt -> IO CString | |
main :: IO () | |
main = do | |
name <- newCString "233" | |
db <- c_wg_attach_database name 1000000000 | |
cstr <- newCString "Hello, world" | |
loop db cstr (10000000 :: Int) | |
c_wg_detach_database db | |
return () | |
where | |
loop db cstr 0 = return () | |
loop db cstr n = do | |
re <- c_wg_create_record db 1 | |
enc <- c_wg_encode_str db cstr 0 | |
c_wg_set_field db re 0 enc | |
loop db cstr (n - 1) |
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
{-# LANGUAGE ForeignFunctionInterface #-} | |
import Foreign | |
import Foreign.C | |
data WhiteDB | |
data Record | |
foreign import ccall unsafe "wg_attach_database" c_wg_attach_database :: CString -> CInt -> IO (Ptr WhiteDB) | |
foreign import ccall unsafe "wg_detach_database" c_wg_detach_database :: Ptr WhiteDB -> IO CInt | |
foreign import ccall unsafe "wg_create_record" c_wg_create_record :: Ptr WhiteDB -> CInt -> IO (Ptr Record) | |
foreign import ccall unsafe "wg_set_field" c_wg_set_field :: Ptr WhiteDB -> Ptr Record -> CInt -> CInt -> IO CInt | |
foreign import ccall unsafe "wg_int wg_encode_str" c_wg_encode_str :: Ptr WhiteDB -> CString -> CInt -> IO CInt | |
foreign import ccall unsafe "wg_int wg_decode_str" c_wg_decode_str :: Ptr WhiteDB -> CInt -> IO CString | |
main :: IO () | |
main = do | |
name <- newCString "233" | |
db <- c_wg_attach_database name 1000000000 | |
cstr <- newCString "Hello, world" | |
loop db cstr (10000000 :: Int) | |
c_wg_detach_database db | |
return () | |
where | |
loop db cstr 0 = return () | |
loop db cstr n = do | |
re <- c_wg_create_record db 1 | |
enc <- c_wg_encode_str db cstr 0 | |
c_wg_set_field db re 0 enc | |
loop db cstr (n - 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment