Python Forum
Socket creation speed difference Python 2.7 / Python 3.7
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Socket creation speed difference Python 2.7 / Python 3.7
#1
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
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python difference between sys.exit and exit() mg24 1 1,764 Nov-12-2022, 01:37 PM
Last Post: deanhystad
  dynamic object creation using python gary 7 1,201 Oct-15-2022, 01:35 PM
Last Post: Larz60+
  Increase the speed of a python loop over a pandas dataframe mcva 0 1,290 Jan-21-2022, 06:24 PM
Last Post: mcva
  how to find difference between fake driving license or real using python? pjaymn 2 2,500 Dec-20-2020, 08:41 PM
Last Post: jefsummers
  changing animation speed using buttons in python microwave 1 2,081 Jun-24-2020, 05:21 PM
Last Post: GOTO10
  python 3 find difference between 2 files pd007 2 2,079 May-22-2020, 01:16 AM
Last Post: Larz60+
  Python module speed or python speed in general Enrique6 1 1,793 May-04-2020, 06:21 PM
Last Post: micseydel
Question Difference between Python's os.system and Perl's system command Agile741 13 6,656 Dec-02-2019, 04:41 PM
Last Post: Agile741
  Python Script to Produce Difference Between Files and Resolve DNS Query for the Outpu sultan 2 2,463 May-22-2019, 07:20 AM
Last Post: buran
  python 2D array creation and print issue developerbrain 5 2,758 May-15-2019, 01:38 PM
Last Post: developerbrain

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020