'What is the difference between struct format characters L (long) and I (int)?
What's the difference between i1 and i2 in Python 3? Why is i1 faster? Is one of the two better?
https://docs.python.org/3/library/struct.html#format-characters
import struct, timeit
print("i1 in %s" % timeit.timeit(
"struct.unpack('<I', b'\\x3C\\x5B\\x01\\x00')",
globals=globals(), number=10000000))
print("i2 in %s" % timeit.timeit(
"struct.unpack('<I', b'\\x3C\\x5B\\x01\\x00')",
globals=globals(), number=10000000))
i1 = struct.unpack('<I', b"\x3C\x5B\x01\x00")[0]
i2 = struct.unpack('<L', b"\x3C\x5B\x01\x00")[0]
print(i1, type(i1), i2, type(i2))
Output:
i1 in 1.336791369
i2 in 1.430974416
88892 <class 'int'> 88892 <class 'int'>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
