Created
July 11, 2019 18:43
-
-
Save danudey/94b5442b617a734a2366e824be159beb to your computer and use it in GitHub Desktop.
An ugly little script to test the affect of various object allocation 'strategies' in Python
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
#!/usr/bin/env python | |
import sys | |
import time | |
from random import randint | |
import gc | |
import subprocess | |
import os | |
from array import ArrayType | |
from ctypes import c_uint32, Structure | |
from contextlib import contextmanager | |
ARR_MAX = 10000000 | |
def get_process_size(): | |
cmd = ["/bin/ps","up",str(format(os.getpid()))] | |
p = subprocess.run(cmd, stdout=subprocess.PIPE) | |
output = p.stdout.decode().split("\n")[1] | |
fields = [a for a in output.split() if a] | |
return tuple(map(int,fields[4:6])) | |
def bytes_to_human(bytes): | |
for factor in ["B","KB","MB","GB","TB"]: | |
if bytes < 1024: | |
break | |
else: | |
bytes = bytes / 1024 | |
return "{bytes:0.2f}{factor}".format(bytes=bytes,factor=factor) | |
def generate_point(x): | |
return x, x | |
@contextmanager | |
def timer(msg): | |
start = time.time() | |
print ("ENTER: {}".format(msg)) | |
try: | |
yield | |
finally: | |
end = time.time() - start | |
print(" END: {} ({:0.2f}s)".format(msg, end)) | |
start = get_process_size() | |
if sys.argv[1] == "list": | |
f = list(range(0,ARR_MAX)) | |
elif sys.argv[1] == "atlist": | |
at = ArrayType("I", range(0,ARR_MAX)) | |
elif sys.argv[1] == "ctypeslist": | |
intarr = c_uint32 * ARR_MAX | |
carr = intarr(*range(0,ARR_MAX)) | |
elif sys.argv[1] == "tuplepoints": | |
f = [(x,x) for x in range(0,ARR_MAX)] | |
elif sys.argv[1] == "ctypespoints": | |
class POINT(Structure): | |
_fields_ = [("x", c_uint32), | |
("y", c_uint32)] | |
arr = POINT * ARR_MAX | |
arr2 = arr() | |
for i in range(0,ARR_MAX): | |
arr2[i] = POINT(i,i) | |
gc.collect() | |
end = get_process_size() | |
print (end[0] - start[0], end[1] - start[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can view an example graph showing the results from my testing. I had assumed that ctypes would be faster and more efficient, but it was not even remotely close in either dimension, so here we are.