Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- from cffi import FFI
- try:
- import _foo_ffi
- except:
- from cffi import FFI
- foo_ffi = FFI()
- foo_ffi.cdef("""
- void* malloc(long);
- void free(void*);
- long strlen(char*);
- """)
- foo_ffi.set_source("_foo_ffi", "")
- foo_ffi.compile()
- import _foo_ffi
- ffi = FFI()
- l = [None]
- t0 = time.time()
- for i in range(10000000):
- with ffi.new("char[]", 1024) as x:
- x[100] = b'\x00'
- _foo_ffi.lib.strlen(x)
- #l[-1] = x
- print("FFI.new %.2f" % (time.time() - t0))
- t1 = time.time()
- for i in range(10000000):
- x = _foo_ffi.lib.malloc(1024)
- ffi.cast("char*", x)[100] = b'\x00'
- _foo_ffi.lib.strlen(x)
- _foo_ffi.lib.free(x)
- print("Raw malloc %.2f" % (time.time() - t1))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement