Skip to content

Conversation

Deneby67
Copy link
Contributor

@Deneby67 Deneby67 commented Feb 4, 2019

Optimized packet reading speed 26 times faster on my pc

26 times faster on my pc
@@ -638,7 +638,7 @@ def _read_packet(self, packet_type=MysqlPacket):
:raise OperationalError: If the connection to the MySQL server is lost.
:raise InternalError: If the packet sequence number is wrong.
"""
buff = b''
buff = io.BytesIO()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try bytearray, instead of BytesIO

@methane
Copy link
Member

methane commented Feb 5, 2019

What is your envirnonment?
I thought bytes += has optimization to avoid exponential execution time.

@Deneby67
Copy link
Contributor Author

Deneby67 commented Feb 5, 2019

What is your envirnonment?
I thought bytes += has optimization to avoid exponential execution time.

+= works str only

Python 3.6.7 (default, Oct 22 2018, 11:32:17) 
[GCC 8.2.0] on linux


import time

def t1():
    t1 = time.time()
    b = b''
    for _ in range(1_000_000):
        b += b'i'
    print(f'concat {time.time() - t1}')

def t2():
    t1 = time.time()
    b = bytearray()
    for _ in range(1_000_000):
        b += b'i'
    print(f'byte array concat {time.time() - t1}')


def t3():
    t1 = time.time()
    b = bytearray()
    for _ in range(1_000_000):
        b.extend(b'i')
    print(f'byte array extend {time.time() - t1}')


t1()
t2()
t3()



concat 36.097856521606445
byte array concat 0.04438519477844238
byte array extend 0.07430744171142578

@methane
Copy link
Member

methane commented Feb 5, 2019

OK, thanks.

@methane methane merged commit 3539f87 into PyMySQL:master Feb 5, 2019
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 31, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants