मैं रेंज में एक ही इंट के लिए विभिन्न तरीकों के प्रदर्शन के बारे में उत्सुक था [0, 255], इसलिए मैंने कुछ समय परीक्षण करने का फैसला किया।
नीचे दिए गए समय के आधार पर, और सामान्य प्रवृत्ति मैं कई विभिन्न मूल्यों और विन्यास की कोशिश कर रहा से मनाया से, struct.packसबसे तेज, जिसके बाद हो रहा है int.to_bytes, bytesऔर साथ str.encode(आश्चर्य) धीमी जा रहा है। ध्यान दें कि परिणाम प्रतिनिधित्व की तुलना में कुछ अधिक भिन्नता दिखाते हैं, int.to_bytesऔर bytesकभी-कभी परीक्षण के दौरान गति रैंकिंग को बदल दिया struct.packजाता है , लेकिन स्पष्ट रूप से सबसे तेज़ है।
विंडोज पर CPython 3.7 में परिणाम:
Testing with 63:
bytes_: 100000 loops, best of 5: 3.3 usec per loop
to_bytes: 100000 loops, best of 5: 2.72 usec per loop
struct_pack: 100000 loops, best of 5: 2.32 usec per loop
chr_encode: 50000 loops, best of 5: 3.66 usec per loop
परीक्षण मॉड्यूल (नाम int_to_byte.py):
"""Functions for converting a single int to a bytes object with that int's value."""
import random
import shlex
import struct
import timeit
def bytes_(i):
"""From Tim Pietzcker's answer:
https://stackoverflow.com/a/21017834/8117067
"""
return bytes([i])
def to_bytes(i):
"""From brunsgaard's answer:
https://stackoverflow.com/a/30375198/8117067
"""
return i.to_bytes(1, byteorder='big')
def struct_pack(i):
"""From Andy Hayden's answer:
https://stackoverflow.com/a/26920966/8117067
"""
return struct.pack('B', i)
# Originally, jfs's answer was considered for testing,
# but the result is not identical to the other methods
# https://stackoverflow.com/a/31761722/8117067
def chr_encode(i):
"""Another method, from Quuxplusone's answer here:
https://codereview.stackexchange.com/a/210789/140921
Similar to g10guang's answer:
https://stackoverflow.com/a/51558790/8117067
"""
return chr(i).encode('latin1')
converters = [bytes_, to_bytes, struct_pack, chr_encode]
def one_byte_equality_test():
"""Test that results are identical for ints in the range [0, 255]."""
for i in range(256):
results = [c(i) for c in converters]
# Test that all results are equal
start = results[0]
if any(start != b for b in results):
raise ValueError(results)
def timing_tests(value=None):
"""Test each of the functions with a random int."""
if value is None:
# random.randint takes more time than int to byte conversion
# so it can't be a part of the timeit call
value = random.randint(0, 255)
print(f'Testing with {value}:')
for c in converters:
print(f'{c.__name__}: ', end='')
# Uses technique borrowed from https://stackoverflow.com/q/19062202/8117067
timeit.main(args=shlex.split(
f"-s 'from int_to_byte import {c.__name__}; value = {value}' " +
f"'{c.__name__}(value)'"
))
3 .to_bytes