Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2021
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.76 KB | None | 0 0
  1. import time
  2. from cffi import FFI
  3. try:
  4.     import _foo_ffi
  5. except:
  6.     from cffi import FFI
  7.     foo_ffi = FFI()
  8.     foo_ffi.cdef("""
  9.        void* malloc(long);
  10.        void free(void*);
  11.        long strlen(char*);
  12.    """)
  13.     foo_ffi.set_source("_foo_ffi", "")
  14.     foo_ffi.compile()
  15.     import _foo_ffi
  16.  
  17. ffi = FFI()
  18. l = [None]
  19. t0 = time.time()
  20. for i in range(10000000):
  21.     with ffi.new("char[]", 1024) as x:
  22.         x[100] = b'\x00'
  23.         _foo_ffi.lib.strlen(x)
  24.     #l[-1] = x
  25.  
  26. print("FFI.new %.2f" % (time.time() - t0))
  27.  
  28. t1 = time.time()
  29.  
  30. for i in range(10000000):
  31.     x = _foo_ffi.lib.malloc(1024)
  32.     ffi.cast("char*", x)[100] = b'\x00'
  33.     _foo_ffi.lib.strlen(x)
  34.     _foo_ffi.lib.free(x)
  35. print("Raw malloc %.2f" % (time.time() - t1))
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement