How to do C language calculation in Python
I want to do some simulation of C language calculation in Python. For example, unsigned short, single precision float ...
ushort(0xffff) + 1 -> 0
0.1f * 0.1f -> ...
Are there some library to do this in Python?
I can use ctypes to create unsigned short, single float, but they cann't do math operation:
a = c_uint16(0xffff)
b = c_uint16(0x01)
a+b -> TypeError
Or, I can use numpy:
>>> np.uint16(0xffff) + np.uint16(0x01)
Warning: overflow encountered in ushort_scalars
0
but it's very slow comparing to Python's normal calculation:
>>> timeit.timeit("a+b", "import numpy as np;a=np.uint16(0xfffe);b=np.uint16(0x01)")
0.35577465681618037
>>> timeit.timeit("0xfffe+0x01")
0.022638104432360251
>>> timeit.timeit("np.uint16(0xfffe) + np.uint16(0x01)", "import numpy as np")
5.9047653992开发者_运维知识库36851
Edit:
>>> timeit.timeit("a+b", "a=0xfffe;b=0x01")
0.040062221014295574
When compiling 0xfffe+0x01
, this will be folded into the constant 65535
. You aren't timing how long the addition takes -- you are just measuring the time of loading the constant:
>>> dis.dis(compile("0xfffe+0x01", "", "eval"))
1 0 LOAD_CONST 2 (65535)
3 RETURN_VALUE
The addition of NumPy scalars is slower than adding built-in integers nevertheless, but it won't get better than that in pure Python. Consider using Cython -- it will allow you to declare types and execute the computations in C speed. Alternatively, try to vectorise your code in NumPy (that is, if speed really matters).
You can make a function for each operation using modulo %
with 2**sizeof
(in your case, 2**16 or 65536)
def add(a, b, mod=2**16):
return (a+b) % mod
def sub(a, b, mod=2**16):
return (a-b) % mod
and any other function you need.
>>> add(0xffff, 1)
0
>>> sub(10, 20)
65526
Note this will work only for unsigned types. For signed ones, you can use half the value used to mod (i.e. 2**15) and will have to validate the result before applying modulo
精彩评论