Last active
August 29, 2015 13:56
-
-
Save lukauskas/8975543 to your computer and use it in GitHub Desktop.
A set of benchmarks for the "Why your Python is slow. Part 1: Data Structures" blogpost
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
{ | |
"metadata": { | |
"name": "" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import random\n" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"N_POINTS = 1000" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def generate_points_as_dicts(count):\n", | |
" random.seed(42)\n", | |
" points = []\n", | |
" for __ in range(count):\n", | |
" x = random.random()\n", | |
" y = random.random()\n", | |
" points.append({'x' : x, 'y': y})\n", | |
" return points\n", | |
" " | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"class Point(object):\n", | |
" x = None\n", | |
" y = None\n", | |
" def __init__(self, x, y):\n", | |
" self.x, self.y = x, y\n", | |
" \n", | |
"def generate_points_as_classes(count):\n", | |
" random.seed(42)\n", | |
" points = []\n", | |
" for __ in range(count):\n", | |
" x = random.random()\n", | |
" y = random.random()\n", | |
" points.append(Point(x,y))\n", | |
" return points" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"class PointWithSlots(object):\n", | |
" __slots__ = ['x', 'y']\n", | |
" def __init__(self, x, y):\n", | |
" self.x, self.y = x, y\n", | |
"\n", | |
"def generate_points_as_classes_with_slots(count):\n", | |
" random.seed(42)\n", | |
" points = []\n", | |
" for __ in range(count):\n", | |
" x = random.random()\n", | |
" y = random.random()\n", | |
" points.append(PointWithSlots(x,y))\n", | |
" return points" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"from collections import namedtuple\n", | |
"PointTuple = namedtuple('PointTuple', ['x', 'y'])\n", | |
"\n", | |
"def generate_points_as_namedtuples(count):\n", | |
" random.seed(42)\n", | |
" points = []\n", | |
" for __ in range(count):\n", | |
" x = random.random()\n", | |
" y = random.random()\n", | |
" points.append(PointTuple(x,y))\n", | |
" return points" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%timeit generate_points_as_dicts(N_POINTS)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%timeit generate_points_as_classes(N_POINTS)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%timeit generate_points_as_classes_with_slots(N_POINTS)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"%timeit generate_points_as_namedtuples(N_POINTS)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def sum_all_points_as_dict(points):\n", | |
" sum_x, sum_y = 0, 0\n", | |
" for point in points:\n", | |
" sum_x += point['x']\n", | |
" sum_y += point['y']\n", | |
" return sum_x, sum_y" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def sum_all_points_as_class(points):\n", | |
" sum_x, sum_y = 0, 0\n", | |
" for point in points:\n", | |
" sum_x += point.x\n", | |
" sum_y += point.y\n", | |
" return sum_x, sum_y" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def sum_all_points_as_namedtuple(points):\n", | |
" sum_x, sum_y = 0, 0\n", | |
" for point in points:\n", | |
" sum_x += point.x\n", | |
" sum_y += point.y\n", | |
" return sum_x, sum_y" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"print \"Dicts:\"\n", | |
"points_dict = generate_points_as_dicts(N_POINTS)\n", | |
"%timeit sum_all_points_as_dict(points_dict)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"print \"Classes: \"\n", | |
"points_class = generate_points_as_classes(N_POINTS)\n", | |
"%timeit sum_all_points_as_class(points_class)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"print \"Classes with __slots__: \"\n", | |
"points_class_with_slots = generate_points_as_classes_with_slots(N_POINTS)\n", | |
"%timeit sum_all_points_as_class(points_class_with_slots)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"print \"Namedtuples: \"\n", | |
"points_namedtuple = generate_points_as_namedtuples(N_POINTS)\n", | |
"%timeit sum_all_points_as_namedtuple(points_namedtuple)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment