Skip to content

Instantly share code, notes, and snippets.

View bhavishyagopesh's full-sized avatar

BHAVISHYA bhavishyagopesh

View GitHub Profile
# 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()
python server.py
# 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
# Utility function for demonstration
def fib(n):
if n <= 2:
return 1
else:
return fib(n-1) + fib(n-2)
@bhavishyagopesh
bhavishyagopesh / fib.py
Created August 17, 2017 16:19
Code for concurrency article.
# Utility function for demonstration
def fib(n):
if n <= 2:
return 1
else:
return fib(n-1) + fib(n-2)
@bhavishyagopesh
bhavishyagopesh / bm_math.py
Created August 16, 2017 17:12
Benchmark math module.
"""
Benchmark math module.
"""
import perf
from six.moves import xrange
import math
@bhavishyagopesh
bhavishyagopesh / bm_smtplib.py
Last active August 19, 2017 08:44
Benchmark smtplib module.
"""
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
@bhavishyagopesh
bhavishyagopesh / bm_zlib.py
Last active August 16, 2017 11:29
Benchmark zlib module.
"""
Benchmark zlib module.
"""
import perf
from six.moves import xrange
import zlib
import binascii
@bhavishyagopesh
bhavishyagopesh / bm_number_crunching.py
Created August 12, 2017 00:32
To check the effect for int vs Long
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()
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.