[PYTHON] Efficient conversion method to byte string

The process of converting list data for serial communication with pyserial into a byte string is I felt it was late, so I made a note of the result of the investigation.

Implementation so far

It's a simple thing that I just want to create a byte string without thinking about anything in particular.

    data = [ _ for _ in xrange(0, 10) ]
    byte_str = "".join([chr(x) for x in send_list])
    # byte_str => '\x00\x01\x02\x03\x04\x05\x06\x07\x08\t'

Processing speed verification

When I was looking for another implementation method, it was written in the stackoverflow article in the reference link I decided to test the process. The version of python is 2 series.

import time
import array
import struct

def main():
    #Method for measurement
    def measure_time(data, func):
        post = time.time()
        for _ in xrange(1000000):
            func(data)
        t = time.time() - post
        return t

    #Byte string conversion method(The processing result is the same for all patterns)
    bytearray_pattern = lambda x : str(bytearray(x))            #pattern 1
    array_pattern = lambda x : array.array(b'B', x).tostring()  #Pattern 2
    struct_pattern = lambda x : struct.pack(b'B' * len(x), *x)  #Pattern 3
    join_pattern = lambda x : "".join([chr(_) for _ in x])      #Pattern 4(Processing so far)

    #Test list data
    small_data = [ _ for _ in xrange(0,10) ]    # len=10
    huge_data = [ _ for _ in xrange(0,256) ]    # len=256

    #Main processing
    proc_time = measure_time(small_data, bytearray_pattern) # proc_time:1.18099999428
    proc_time = measure_time(small_data, array_pattern)     # proc_time:0.874000072479
    proc_time = measure_time(small_data, struct_pattern)    # proc_time:0.784999847412
    proc_time = measure_time(small_data, join_pattern)      # proc_time:1.72000002861
    proc_time = measure_time(huge_data, bytearray_pattern)  # proc_time:5.57999992371
    proc_time = measure_time(huge_data, array_pattern)      # proc_time:11.3169999123
    proc_time = measure_time(huge_data, struct_pattern)     # proc_time:11.0810000896
    proc_time = measure_time(huge_data, join_pattern)       # proc_time:32.9349999428

main()

result

If you have a lot of data to send, it seems better to use pattern 1 str (bytearray (list). Also, when the number of data is small, it seems that pattern 2 and pattern 3 may be faster. (It looks like an error)

For the time being, what I can say for sure is that there is no pattern 4 ... (Reflection)

Reference link

http://stackoverflow.com/questions/3470398/list-of-integers-into-string-byte-array-python

Recommended Posts

Efficient conversion method to byte string
Hexadecimal string and string conversion
Introduction to Monte Carlo Method
Convert hexadecimal string to binary
[python] Convert date to string