This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Time of a long running request | |
from socket import * | |
import time | |
sock = socket(AF_INET, SOCK_STREAM) | |
sock.connect(('localhost', 25000)) | |
while True: | |
start = time.time() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
python server.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# server code | |
from fib import fib | |
from socket import * | |
def fib_server(address): | |
# This sets up the socket(you can think that of as pipe over which communication happens) object | |
sock = socket(AF_INET, SOCK_STREAM) | |
sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) | |
# It binds to the address and listens for any connections |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Utility function for demonstration | |
def fib(n): | |
if n <= 2: | |
return 1 | |
else: | |
return fib(n-1) + fib(n-2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Utility function for demonstration | |
def fib(n): | |
if n <= 2: | |
return 1 | |
else: | |
return fib(n-1) + fib(n-2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Benchmark math module. | |
""" | |
import perf | |
from six.moves import xrange | |
import math | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Benchmark smtplib module. | |
""" | |
# This benchmark requires a running smtp service on port 25 | |
# For Eg: PostFix | |
import perf | |
from six.moves import xrange | |
import smtplib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Benchmark zlib module. | |
""" | |
import perf | |
from six.moves import xrange | |
import zlib | |
import binascii |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import perf | |
from six.moves import xrange | |
def add_cmdline_args(cmd, args): | |
if args.benchmark: | |
cmd.append(args.benchmark) | |
CRUNCH_NO=10000000 | |
def bench_number_crunching(loops): | |
range_it = xrange(loops) | |
t0 = perf.perf_counter() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The above script tries to benchmark "Concurrency" implemented using threading and multiprocessing.Actually "threads" in cpython are restricted by "GIL" | |
,so it's not actually concurrent...On the other hand "multiprocessing" module creates whole different processes but there is substaintial cost involved in | |
spawing a whole new process. | |
So the there is a trade-off involved which is evident as we increase "CRUNCH_NO" variable. | |
But the benchmark actually tries to compare the same phenomenon in py2 and py3.And py2 looks faster here. | |
I'm adding two graphs comparing the timings for py2 and py3. |