Python Forum

Full Version: Socket creation speed difference Python 2.7 / Python 3.7
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I'm trying to create a socket, then connect to the same server until I can't make any connection from my PC and I face a problem : my program is WAY faster with Python 2.7 than with Python 3.7.

Here a minimal example:

# client.py

import time
import socket

begin = time.time()
socket_list = []

while True:
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.settimeout(1)
        s.connect(("127.0.0.1", 65432))
        socket_list.append(s)
    except:
        print(len(socket_list))
        print(time.time() - begin)
        for sock in socket_list:
            sock.close()
        break
# server.py

import socket 

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("127.0.0.1", 65432))
s.listen()
while True:
    s.accept()


Ouput with Python 3.7 client side and Python 3.7 server side:

16298
32.764869928359985

Ouput with Python 2.7 client side and Python 3.7 server side:

16297
1.86599993706

OS: Windows 10
Removing the line
s.settimeout(1)
solved the problem, I now have similar performance with Python 2.7 and Python 3.7. Why, I don't know.