szha opened a new pull request #8642: ctypes speed improvement URL: https://github.com/apache/incubator-mxnet/pull/8642 ## Description ## Improvements on ctypes calls for creating arrays. The changes are based on the following micro benchmarks. ```python import timeit # ints setup="from array import array; import ctypes; t = [i for i in range(1000000)];" print('int before', timeit.timeit(stmt='(ctypes.c_uint32 * len(t))(*t)',setup=setup,number=10)) print('int after', timeit.timeit(stmt="v = array('I',t);a = (ctypes.c_uint32 * len(v)).from_buffer(v)",setup=setup,number=10)) setup=""" import ctypes t = ['abc' for _ in range(100000)] import sys if sys.version_info[0] < 3: def c_str(string): return ctypes.c_char_p(string) def c_str_array(strings): arr = (ctypes.c_char_p * len(strings))() arr[:] = strings return arr else: def c_str(string): return ctypes.c_char_p(string.encode('utf-8')) def c_str_array(strings): arr = (ctypes.c_char_p * len(strings))() arr[:] = [s.encode('utf-8') for s in strings] return arr def c_array(ctype, values): return (ctype * len(values))(*values) def c_array_objs(L): arr = (ctypes.c_void_p * len(L))() arr[:] = [s.handle for s in L] return arr """ # strings print('strings before', timeit.timeit(stmt='r = c_array(ctypes.c_char_p, [c_str(s) for s in t]); assert r[24601].decode("utf-8") == "abc"',setup=setup,number=10)) print('strings after', timeit.timeit(stmt='r = c_str_array(t); assert r[24601].decode("utf-8") == "abc"',setup=setup,number=10)) setup=""" import ctypes import mxnet as mx t = [mx.nd.array([i]) for i in range(100000)] def c_array(ctype, values): return (ctype * len(values))(*values) def c_array_objs(L): arr = (ctypes.c_void_p * len(L))() arr[:] = [s.handle for s in L] return arr """ # arrays print('handles before', timeit.timeit(stmt='r = c_array(mx.base.NDArrayHandle, [i.handle for i in t])',setup=setup,number=10)) print('handles after', timeit.timeit(stmt='r = c_array_objs(t)',setup=setup,number=10)) ``` Results (time in seconds, the lower the better) | python version | int before | int after | strings before | strings after | handles before | handles after | |:----------------:|------------:|-----------:|----------------:|---------------:|----------------:|---------------:| | 2.7.13 | 2.50 | 0.54 | 1.52 | 0.30 | 0.46 | 0.23 | | 3.6.2 | 2.36 | 0.13 | 1.13 | 0.58 | 0.32 | 0.12 | ## Checklist ## ### Essentials ### - [x] Passed code style checking (`make lint`) - [x] Changes are complete (i.e. I finished coding on this PR) - [x] All changes have test coverage - [x] For user-facing API changes, API doc string has been updated. For new C++ functions in header files, their functionalities and arguments are well-documented. - [x] To my best knowledge, examples are either not affected by this change, or have been fixed to be compatible with this change ### Changes ### - [x] update ctypes `c_array` calls
---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services