结构空字节与 \x00
Struct null byte vs \x00
写一个空字节有什么区别:
print("\x00")
并写一个:
print(struct.pack("B", 0))
我冒昧地将它们的执行时间安排如下:
def struct_exec_time():
start_time = time.time()
import struct
print(struct.pack("B",0))
return time.time() - start_time
def simple_print_exec():
start_time = time.time()
print("\x00")
return time.time() - start_time
当运行将它们都固定时:
>>> for _ in range(1):
... print("Struct execution time: {}".format(struct_exec_time()))
... print("Simple print execution time: {}".format(simple_print_exec()))
...
Struct execution time: 2.38418579102e-05
Simple print execution time: 3.09944152832e-06
>>>
似乎 struct 比第一次执行的 print 函数更快,因为如果你 运行 它们不止一次:
>>> for _ in range(5):
... print("Struct execution time: {}".format(struct_exec_time()))
... print("Simple print execution time: {}".format(simple_print_exec()))
...
Struct execution time: 2.71797180176e-05
Simple print execution time: 5.00679016113e-06
Struct execution time: 9.05990600586e-06
Simple print execution time: 4.05311584473e-06
Struct execution time: 7.15255737305e-06
Simple print execution time: 5.00679016113e-06
Struct execution time: 7.15255737305e-06
Simple print execution time: 4.05311584473e-06
Struct execution time: 6.91413879395e-06
Simple print execution time: 4.76837158203e-06
那么,两者有什么区别,为什么 struct 只比打印一次快?
编辑:
从计时器中取出 import struct
调用:
def struct_exec_time():
import struct
start_time = time.time()
print(struct.pack("B",0))
return time.time() - start_time
for _ in range(5):
print("Struct exec: {}".format(struct_exec_time()))
print("Print exec: {}".format(simple_print_exec()))
Struct exec: 3.40938568115e-05
Print exec: 2.86102294922e-06
Struct exec: 2.86102294922e-06
Print exec: 3.09944152832e-06
Struct exec: 2.86102294922e-06
Print exec: 3.09944152832e-06
Struct exec: 3.81469726562e-06
Print exec: 3.09944152832e-06
Struct exec: 2.86102294922e-06
Print exec: 2.14576721191e-06
它们不一样。
print("\x00")
这将打印 unicode 代码点 0; unicode 将被解码为您终端的编码(默认为 utf-8),相应的字节将被发送到您进程的 stdout
print(struct.pack("B", 0))
这将打印空字节表示。由于 struct.pack() 的结果是字节串,python 不会尝试对其进行编码,而 print()
会将其转换为表示形式:
>>> print('\x00')
>>> import struct
>>> print(struct.pack("B", 0))
b'\x00'
如果您使用的是 Python 2.7,则两个值相等且类型相同:str
(= Python 3 bytes
).
这是一个 python 2/3 测试:
import struct
b1 = struct.pack('B', 0)
b2 = b'\x00'
assert b1 == b2
assert type(b1) == type(b2)
在日常编程中,我更喜欢使用字节字符串而不是使用 struct
。
引用文档:
This module performs conversions between Python values and C structs represented as Python bytes objects.
编辑
关于性能的注意事项:b'\x00' 是文字。与函数调用相比,文字的计算总是更快。
写一个空字节有什么区别:
print("\x00")
并写一个:
print(struct.pack("B", 0))
我冒昧地将它们的执行时间安排如下:
def struct_exec_time():
start_time = time.time()
import struct
print(struct.pack("B",0))
return time.time() - start_time
def simple_print_exec():
start_time = time.time()
print("\x00")
return time.time() - start_time
当运行将它们都固定时:
>>> for _ in range(1):
... print("Struct execution time: {}".format(struct_exec_time()))
... print("Simple print execution time: {}".format(simple_print_exec()))
...
Struct execution time: 2.38418579102e-05
Simple print execution time: 3.09944152832e-06
>>>
似乎 struct 比第一次执行的 print 函数更快,因为如果你 运行 它们不止一次:
>>> for _ in range(5):
... print("Struct execution time: {}".format(struct_exec_time()))
... print("Simple print execution time: {}".format(simple_print_exec()))
...
Struct execution time: 2.71797180176e-05
Simple print execution time: 5.00679016113e-06
Struct execution time: 9.05990600586e-06
Simple print execution time: 4.05311584473e-06
Struct execution time: 7.15255737305e-06
Simple print execution time: 5.00679016113e-06
Struct execution time: 7.15255737305e-06
Simple print execution time: 4.05311584473e-06
Struct execution time: 6.91413879395e-06
Simple print execution time: 4.76837158203e-06
那么,两者有什么区别,为什么 struct 只比打印一次快?
编辑:
从计时器中取出 import struct
调用:
def struct_exec_time():
import struct
start_time = time.time()
print(struct.pack("B",0))
return time.time() - start_time
for _ in range(5):
print("Struct exec: {}".format(struct_exec_time()))
print("Print exec: {}".format(simple_print_exec()))
Struct exec: 3.40938568115e-05
Print exec: 2.86102294922e-06
Struct exec: 2.86102294922e-06
Print exec: 3.09944152832e-06
Struct exec: 2.86102294922e-06
Print exec: 3.09944152832e-06
Struct exec: 3.81469726562e-06
Print exec: 3.09944152832e-06
Struct exec: 2.86102294922e-06
Print exec: 2.14576721191e-06
它们不一样。
print("\x00")
这将打印 unicode 代码点 0; unicode 将被解码为您终端的编码(默认为 utf-8),相应的字节将被发送到您进程的 stdout
print(struct.pack("B", 0))
这将打印空字节表示。由于 struct.pack() 的结果是字节串,python 不会尝试对其进行编码,而 print()
会将其转换为表示形式:
>>> print('\x00')
>>> import struct
>>> print(struct.pack("B", 0))
b'\x00'
如果您使用的是 Python 2.7,则两个值相等且类型相同:str
(= Python 3 bytes
).
这是一个 python 2/3 测试:
import struct
b1 = struct.pack('B', 0)
b2 = b'\x00'
assert b1 == b2
assert type(b1) == type(b2)
在日常编程中,我更喜欢使用字节字符串而不是使用 struct
。
引用文档:
This module performs conversions between Python values and C structs represented as Python bytes objects.
编辑
关于性能的注意事项:b'\x00' 是文字。与函数调用相比,文字的计算总是更快。