moved ipynb

This commit is contained in:
rasbt 2014-04-14 14:28:42 -04:00
parent 1fb82d58f3
commit 3f5b486d18
2 changed files with 617 additions and 103 deletions

View File

@ -1,7 +1,7 @@
{
"metadata": {
"name": "",
"signature": "sha256:e6a1d9a637dfbad45211a98a5bbc35255dc8f168a834ed466c363dfc384c0f59"
"signature": "sha256:5a2264b30b9632e14bd425a887a4455658fbdf9f8102fc5703ad982c3fa09b21"
},
"nbformat": 3,
"nbformat_minor": 0,
@ -12,6 +12,18 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Sebastian Raschka \n",
"last updated: 04/14/2014 \n",
"\n",
"[Link to this IPython Notebook on GitHub](https://github.com/rasbt/python_reference/blob/master/timeit_test.ipynb)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"\n",
"# Python benchmarks via `timeit`"
]
},
@ -20,12 +32,15 @@
"metadata": {},
"source": [
"# Sections\n",
"- [String formatting](#string_formatting)\n",
" - [String formatting - .format() vs. binary operator %s](#str_format_bin)\n",
" - [String Reversing - [::-1] vs. `''.join(reversed())`](#str_reverse)\n",
"- [List Operations](#list_operations)\n",
" - [List Reversing - [::-1] vs. reverse() vs. reversed()](#list_reverse)\n",
"- [Dictionary Operations](#dict_ops) \n",
"- [String operations](#string_operations)\n",
" - [String formatting: .format() vs. binary operator %s](#str_format_bin)\n",
" - [String reversing: [::-1] vs. `''.join(reversed())`](#str_reverse)\n",
" - [String concatenation: `+=` vs. `''.join()`](#string_concat)\n",
" - [Assembling strings](#string_assembly) \n",
"- [List operations](#list_operations)\n",
" - [List reversing: [::-1] vs. reverse() vs. reversed()](#list_reverse)\n",
" - [Creating lists using conditional statements](#create_cond_list)\n",
"- [Dictionary operations](#dict_ops) \n",
" - [Adding elements to a dictionary](#adding_dict_elements)"
]
},
@ -33,9 +48,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name='string_formatting'></a>\n",
"<a name='string_operations'></a>\n",
"\n",
"# String formatting"
"# String operations"
]
},
{
@ -43,7 +58,9 @@
"metadata": {},
"source": [
"<a name='str_format_bin'></a>\n",
"### String formatting - `.format()` vs. binary operator `%s`"
"## String formatting: `.format()` vs. binary operator `%s`\n",
"\n",
"We expect the string .format() method to perform slower than %, because it is doing the formatting for each object itself, where formatting via the binary % is hard-coded for known types. But let's see how big the difference really is..."
]
},
{
@ -61,11 +78,6 @@
"%timeit test_format()\n",
"%timeit test_binaryop()\n",
"\n",
"#print('{}: {}\\n{}: {}'.format('format()', format_res, '%s', binaryop_res))\n",
"\n",
"################################\n",
"# On my machine\n",
"################################\n",
"#\n",
"# Python 3.4.0\n",
"# MacOS X 10.9.2\n",
@ -99,7 +111,7 @@
"metadata": {},
"source": [
"<a name='str_reverse'></a>\n",
"### String Reversing - `[::-1]` vs. `''.join(reversed())`"
"## String reversing: `[::-1]` vs. `''.join(reversed())`"
]
},
{
@ -150,12 +162,140 @@
],
"prompt_number": 13
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name='string_concat'></a>\n",
"## String concatenation: `+=` vs. `''.join()`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Strings in Python are immutable objects. So, each time we append a character to a string, it has to be created \u201cfrom scratch\u201d in memory. Thus, the answer to the question \u201cWhat is the most efficient way to concatenate strings?\u201d is a quite obvious, but the relative numbers of performance gains are nonetheless interesting."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import timeit\n",
"\n",
"def string_add(in_chars):\n",
" new_str = ''\n",
" for char in in_chars:\n",
" new_str += char\n",
" return new_str\n",
"\n",
"def string_join(in_chars):\n",
" return ''.join(in_chars)\n",
"\n",
"test_chars = ['a', 'b', 'c', 'd', 'e', 'f']\n",
"\n",
"%timeit string_add(test_chars)\n",
"%timeit string_join(test_chars)\n",
"\n",
"#\n",
"# Python 3.4.0\n",
"# MacOS X 10.9.2\n",
"# 2.5 GHz Intel Core i5\n",
"# 4 GB 1600 Mhz DDR3\n",
"#"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"1000000 loops, best of 3: 595 ns per loop\n",
"1000000 loops, best of 3: 269 ns per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 16
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name='string_assembly'></a>\n",
"## Assembling strings\n",
"\n",
"Next, I wanted to compare different methods string \u201cassembly.\u201d This is different from simple string concatenation, which we have seen in the previous section, since we insert values into a string, e.g., from a variable."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import timeit\n",
"\n",
"def plus_operator():\n",
" return 'a' + str(1) + str(2) \n",
" \n",
"def format_method():\n",
" return 'a{}{}'.format(1,2)\n",
" \n",
"def binary_operator():\n",
" return 'a%s%s' %(1,2)\n",
"\n",
"%timeit plus_operator()\n",
"%timeit format_method()\n",
"%timeit binary_operator()\n",
"\n",
"#\n",
"# Python 3.4.0\n",
"# MacOS X 10.9.2\n",
"# 2.5 GHz Intel Core i5\n",
"# 4 GB 1600 Mhz DDR3\n",
"#"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"1000000 loops, best of 3: 764 ns per loop\n",
"1000000 loops, best of 3: 494 ns per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"10000000 loops, best of 3: 79.3 ns per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 17
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name='list_operations'></a>\n",
"# List Operations"
"# List operations"
]
},
{
@ -163,7 +303,7 @@
"metadata": {},
"source": [
"<a name='list_reverse'></a>\n",
"### List Reversing - `[::-1]` vs. `reverse()` vs. `reversed()`"
"## List reversing - `[::-1]` vs. `reverse()` vs. `reversed()`"
]
},
{
@ -222,12 +362,91 @@
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name='create_cond_list'></a>\n",
"## Creating lists using conditional statements\n",
"\n",
"In this test, I attempted to figure out the fastest way to create a new list of elements that meet a certain criterion. For the sake of simplicity, the criterion was to check if an element is even or odd, and only if the element was even, it should be included in the list. For example, the resulting list for numbers in the range from 1 to 10 would be \n",
"[2, 4, 6, 8, 10].\n",
"\n",
"Here, I tested three different approaches: \n",
"1) a simple for loop with an if-statement check (`cond_loop()`) \n",
"2) a list comprehension (`list_compr()`) \n",
"3) the built-in filter() function (`filter_func()`) \n",
"\n",
"Note that the filter() function now returns a generator in Python 3, so I had to wrap it in an additional list() function call."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import timeit\n",
"\n",
"def cond_loop():\n",
" even_nums = []\n",
" for i in range(100):\n",
" if i % 2 == 0:\n",
" even_nums.append(i)\n",
" return even_nums\n",
"\n",
"def list_compr():\n",
" even_nums = [i for i in range(100) if i % 2 == 0]\n",
" return even_nums\n",
" \n",
"def filter_func():\n",
" even_nums = list(filter((lambda x: x % 2 != 0), range(100)))\n",
" return even_nums\n",
"\n",
"%timeit cond_loop()\n",
"%timeit list_compr()\n",
"%timeit filter_func()\n",
"\n",
"#\n",
"# Python 3.4.0\n",
"# MacOS X 10.9.2\n",
"# 2.5 GHz Intel Core i5\n",
"# 4 GB 1600 Mhz DDR3\n",
"#"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"100000 loops, best of 3: 14.4 \u00b5s per loop\n",
"100000 loops, best of 3: 12 \u00b5s per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"10000 loops, best of 3: 23.9 \u00b5s per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 14
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name='dict_ops'></a>\n",
"# Dictionary Operations "
"# Dictionary operations "
]
},
{
@ -235,7 +454,11 @@
"metadata": {},
"source": [
"<a name='adding_dict_elements'></a>\n",
"## Adding elements to a Dictionary"
"## Adding elements to a Dictionary\n",
"\n",
"All three functions below count how often different elements (values) occur in a list. \n",
"E.g., for the list ['a', 'b', 'a', 'c'], the dictionary would look like this: \n",
"`my_dict = {'a': 2, 'b': 1, 'c': 1}`"
]
},
{
@ -243,9 +466,10 @@
"collapsed": false,
"input": [
"import random\n",
"import copy\n",
"import timeit\n",
"\n",
"\n",
"random.seed(123)\n",
"rand_ints = [randrange(1, 10) for i in range(100)]\n",
"\n",
"def add_element_check1(my_dict, elements):\n",
" for e in elements:\n",
@ -260,13 +484,45 @@
" my_dict[e] = 0\n",
" my_dict[e] += 1 \n",
"\n",
"def add_element_except(my_dict):\n",
"def add_element_except(my_dict, elements):\n",
" for e in elements:\n",
" try:\n",
" elements[e] += 1\n",
" my_dict[e] += 1\n",
" except KeyError:\n",
" elements[e] = 1\n",
" "
" my_dict[e] = 1\n",
" \n",
"\n",
"random.seed(123)\n",
"rand_ints = [random.randrange(1, 10) for i in range(100)]\n",
"empty_dict = {}\n",
"\n",
"print('Results for 100 integers in range 1-10') \n",
"%timeit add_element_check1(copy.deepcopy(empty_dict), rand_ints)\n",
"%timeit add_element_check2(copy.deepcopy(empty_dict), rand_ints)\n",
"%timeit add_element_except(copy.deepcopy(empty_dict), rand_ints)\n",
" \n",
"print('\\nResults for 1000 integers in range 1-10') \n",
"rand_ints = [random.randrange(1, 10) for i in range(1000)]\n",
"empty_dict = {}\n",
"\n",
"%timeit add_element_check1(copy.deepcopy(empty_dict), rand_ints)\n",
"%timeit add_element_check2(copy.deepcopy(empty_dict), rand_ints)\n",
"%timeit add_element_except(copy.deepcopy(empty_dict), rand_ints)\n",
"\n",
"print('\\nResults for 1000 integers in range 1-1000') \n",
"rand_ints = [random.randrange(1, 10) for i in range(1000)]\n",
"empty_dict = {}\n",
"\n",
"%timeit add_element_check1(copy.deepcopy(empty_dict), rand_ints)\n",
"%timeit add_element_check2(copy.deepcopy(empty_dict), rand_ints)\n",
"%timeit add_element_except(copy.deepcopy(empty_dict), rand_ints)\n",
"\n",
"#\n",
"# Python 3.4.0\n",
"# MacOS X 10.9.2\n",
"# 2.5 GHz Intel Core i5\n",
"# 4 GB 1600 Mhz DDR3\n",
"#"
],
"language": "python",
"metadata": {},
@ -275,49 +531,95 @@
"output_type": "stream",
"stream": "stdout",
"text": [
"1\n",
"5\n",
"2\n",
"7\n",
"5\n",
"2\n",
"1\n",
"7\n",
"9\n",
"9\n",
"Results for 100 integers in range 1-10\n",
"100000 loops, best of 3: 16.6 \u00b5s per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"100000 loops, best of 3: 17.6 \u00b5s per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"100000 loops, best of 3: 17.9 \u00b5s per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
"6\n",
"6\n",
"1\n",
"3\n",
"3\n",
"6\n",
"9\n",
"6\n",
"4\n",
"3\n"
"Results for 1000 integers in range 1-10\n",
"10000 loops, best of 3: 135 \u00b5s per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"10000 loops, best of 3: 125 \u00b5s per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"10000 loops, best of 3: 105 \u00b5s per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\n",
"Results for 1000 integers in range 1-1000\n",
"10000 loops, best of 3: 122 \u00b5s per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"10000 loops, best of 3: 123 \u00b5s per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"10000 loops, best of 3: 104 \u00b5s per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 16
"prompt_number": 13
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"prompt_number": 15
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
"source": [
"### Conclusion\n",
"Interestingly, the `try-except` loop pays off if we have more elements (here: 1000 integers instead of 100) as dictionary keys to check. Also, it doesn't matter much whether the elements exist or do not exist in the dictionary, yet."
]
}
],
"metadata": {}

View File

@ -1,7 +1,7 @@
{
"metadata": {
"name": "",
"signature": "sha256:629f08ba90badbccee0e3581db0e76c716d0b93abc92869d2dc5176ac6b02854"
"signature": "sha256:5a2264b30b9632e14bd425a887a4455658fbdf9f8102fc5703ad982c3fa09b21"
},
"nbformat": 3,
"nbformat_minor": 0,
@ -12,6 +12,18 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Sebastian Raschka \n",
"last updated: 04/14/2014 \n",
"\n",
"[Link to this IPython Notebook on GitHub](https://github.com/rasbt/python_reference/blob/master/timeit_test.ipynb)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"\n",
"# Python benchmarks via `timeit`"
]
},
@ -20,12 +32,15 @@
"metadata": {},
"source": [
"# Sections\n",
"- [String formatting](#string_formatting)\n",
" - [String formatting - .format() vs. binary operator %s](#str_format_bin)\n",
" - [String Reversing - [::-1] vs. `''.join(reversed())`](#str_reverse)\n",
"- [List Operations](#list_operations)\n",
" - [List Reversing - [::-1] vs. reverse() vs. reversed()](#list_reverse)\n",
"- [Dictionary Operations](#dict_ops) \n",
"- [String operations](#string_operations)\n",
" - [String formatting: .format() vs. binary operator %s](#str_format_bin)\n",
" - [String reversing: [::-1] vs. `''.join(reversed())`](#str_reverse)\n",
" - [String concatenation: `+=` vs. `''.join()`](#string_concat)\n",
" - [Assembling strings](#string_assembly) \n",
"- [List operations](#list_operations)\n",
" - [List reversing: [::-1] vs. reverse() vs. reversed()](#list_reverse)\n",
" - [Creating lists using conditional statements](#create_cond_list)\n",
"- [Dictionary operations](#dict_ops) \n",
" - [Adding elements to a dictionary](#adding_dict_elements)"
]
},
@ -33,9 +48,9 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name='string_formatting'></a>\n",
"<a name='string_operations'></a>\n",
"\n",
"# String formatting"
"# String operations"
]
},
{
@ -43,7 +58,9 @@
"metadata": {},
"source": [
"<a name='str_format_bin'></a>\n",
"### String formatting - `.format()` vs. binary operator `%s`"
"## String formatting: `.format()` vs. binary operator `%s`\n",
"\n",
"We expect the string .format() method to perform slower than %, because it is doing the formatting for each object itself, where formatting via the binary % is hard-coded for known types. But let's see how big the difference really is..."
]
},
{
@ -61,11 +78,6 @@
"%timeit test_format()\n",
"%timeit test_binaryop()\n",
"\n",
"#print('{}: {}\\n{}: {}'.format('format()', format_res, '%s', binaryop_res))\n",
"\n",
"################################\n",
"# On my machine\n",
"################################\n",
"#\n",
"# Python 3.4.0\n",
"# MacOS X 10.9.2\n",
@ -99,7 +111,7 @@
"metadata": {},
"source": [
"<a name='str_reverse'></a>\n",
"### String Reversing - `[::-1]` vs. `''.join(reversed())`"
"## String reversing: `[::-1]` vs. `''.join(reversed())`"
]
},
{
@ -150,12 +162,140 @@
],
"prompt_number": 13
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name='string_concat'></a>\n",
"## String concatenation: `+=` vs. `''.join()`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Strings in Python are immutable objects. So, each time we append a character to a string, it has to be created \u201cfrom scratch\u201d in memory. Thus, the answer to the question \u201cWhat is the most efficient way to concatenate strings?\u201d is a quite obvious, but the relative numbers of performance gains are nonetheless interesting."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import timeit\n",
"\n",
"def string_add(in_chars):\n",
" new_str = ''\n",
" for char in in_chars:\n",
" new_str += char\n",
" return new_str\n",
"\n",
"def string_join(in_chars):\n",
" return ''.join(in_chars)\n",
"\n",
"test_chars = ['a', 'b', 'c', 'd', 'e', 'f']\n",
"\n",
"%timeit string_add(test_chars)\n",
"%timeit string_join(test_chars)\n",
"\n",
"#\n",
"# Python 3.4.0\n",
"# MacOS X 10.9.2\n",
"# 2.5 GHz Intel Core i5\n",
"# 4 GB 1600 Mhz DDR3\n",
"#"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"1000000 loops, best of 3: 595 ns per loop\n",
"1000000 loops, best of 3: 269 ns per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 16
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name='string_assembly'></a>\n",
"## Assembling strings\n",
"\n",
"Next, I wanted to compare different methods string \u201cassembly.\u201d This is different from simple string concatenation, which we have seen in the previous section, since we insert values into a string, e.g., from a variable."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import timeit\n",
"\n",
"def plus_operator():\n",
" return 'a' + str(1) + str(2) \n",
" \n",
"def format_method():\n",
" return 'a{}{}'.format(1,2)\n",
" \n",
"def binary_operator():\n",
" return 'a%s%s' %(1,2)\n",
"\n",
"%timeit plus_operator()\n",
"%timeit format_method()\n",
"%timeit binary_operator()\n",
"\n",
"#\n",
"# Python 3.4.0\n",
"# MacOS X 10.9.2\n",
"# 2.5 GHz Intel Core i5\n",
"# 4 GB 1600 Mhz DDR3\n",
"#"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"1000000 loops, best of 3: 764 ns per loop\n",
"1000000 loops, best of 3: 494 ns per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"10000000 loops, best of 3: 79.3 ns per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 17
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name='list_operations'></a>\n",
"# List Operations"
"# List operations"
]
},
{
@ -163,7 +303,7 @@
"metadata": {},
"source": [
"<a name='list_reverse'></a>\n",
"### List Reversing - `[::-1]` vs. `reverse()` vs. `reversed()`"
"## List reversing - `[::-1]` vs. `reverse()` vs. `reversed()`"
]
},
{
@ -222,12 +362,91 @@
],
"prompt_number": 1
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name='create_cond_list'></a>\n",
"## Creating lists using conditional statements\n",
"\n",
"In this test, I attempted to figure out the fastest way to create a new list of elements that meet a certain criterion. For the sake of simplicity, the criterion was to check if an element is even or odd, and only if the element was even, it should be included in the list. For example, the resulting list for numbers in the range from 1 to 10 would be \n",
"[2, 4, 6, 8, 10].\n",
"\n",
"Here, I tested three different approaches: \n",
"1) a simple for loop with an if-statement check (`cond_loop()`) \n",
"2) a list comprehension (`list_compr()`) \n",
"3) the built-in filter() function (`filter_func()`) \n",
"\n",
"Note that the filter() function now returns a generator in Python 3, so I had to wrap it in an additional list() function call."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import timeit\n",
"\n",
"def cond_loop():\n",
" even_nums = []\n",
" for i in range(100):\n",
" if i % 2 == 0:\n",
" even_nums.append(i)\n",
" return even_nums\n",
"\n",
"def list_compr():\n",
" even_nums = [i for i in range(100) if i % 2 == 0]\n",
" return even_nums\n",
" \n",
"def filter_func():\n",
" even_nums = list(filter((lambda x: x % 2 != 0), range(100)))\n",
" return even_nums\n",
"\n",
"%timeit cond_loop()\n",
"%timeit list_compr()\n",
"%timeit filter_func()\n",
"\n",
"#\n",
"# Python 3.4.0\n",
"# MacOS X 10.9.2\n",
"# 2.5 GHz Intel Core i5\n",
"# 4 GB 1600 Mhz DDR3\n",
"#"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"100000 loops, best of 3: 14.4 \u00b5s per loop\n",
"100000 loops, best of 3: 12 \u00b5s per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"10000 loops, best of 3: 23.9 \u00b5s per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 14
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a name='dict_ops'></a>\n",
"# Dictionary Operations "
"# Dictionary operations "
]
},
{
@ -274,7 +493,7 @@
" \n",
"\n",
"random.seed(123)\n",
"rand_ints = [randrange(1, 10) for i in range(100)]\n",
"rand_ints = [random.randrange(1, 10) for i in range(100)]\n",
"empty_dict = {}\n",
"\n",
"print('Results for 100 integers in range 1-10') \n",
@ -283,7 +502,7 @@
"%timeit add_element_except(copy.deepcopy(empty_dict), rand_ints)\n",
" \n",
"print('\\nResults for 1000 integers in range 1-10') \n",
"rand_ints = [randrange(1, 10) for i in range(1000)]\n",
"rand_ints = [random.randrange(1, 10) for i in range(1000)]\n",
"empty_dict = {}\n",
"\n",
"%timeit add_element_check1(copy.deepcopy(empty_dict), rand_ints)\n",
@ -291,17 +510,18 @@
"%timeit add_element_except(copy.deepcopy(empty_dict), rand_ints)\n",
"\n",
"print('\\nResults for 1000 integers in range 1-1000') \n",
"rand_ints = [randrange(1, 10) for i in range(1000)]\n",
"rand_ints = [random.randrange(1, 10) for i in range(1000)]\n",
"empty_dict = {}\n",
"\n",
"%timeit add_element_check1(copy.deepcopy(empty_dict), rand_ints)\n",
"%timeit add_element_check2(copy.deepcopy(empty_dict), rand_ints)\n",
"%timeit add_element_except(copy.deepcopy(empty_dict), rand_ints)\n",
"\n",
"#\n",
"# Python 3.4.0\n",
"# MacOS X 10.9.2\n",
"# 2.4 GHz Intel Core Duo\n",
"# 8 GB 1067 Mhz DDR3\n",
"# 2.5 GHz Intel Core i5\n",
"# 4 GB 1600 Mhz DDR3\n",
"#"
],
"language": "python",
@ -312,7 +532,7 @@
"stream": "stdout",
"text": [
"Results for 100 integers in range 1-10\n",
"10000 loops, best of 3: 30.5 \u00b5s per loop"
"100000 loops, best of 3: 16.6 \u00b5s per loop"
]
},
{
@ -320,7 +540,7 @@
"stream": "stdout",
"text": [
"\n",
"10000 loops, best of 3: 32.4 \u00b5s per loop"
"100000 loops, best of 3: 17.6 \u00b5s per loop"
]
},
{
@ -328,7 +548,7 @@
"stream": "stdout",
"text": [
"\n",
"10000 loops, best of 3: 31.7 \u00b5s per loop"
"100000 loops, best of 3: 17.9 \u00b5s per loop"
]
},
{
@ -338,7 +558,7 @@
"\n",
"\n",
"Results for 1000 integers in range 1-10\n",
"1000 loops, best of 3: 241 \u00b5s per loop"
"10000 loops, best of 3: 135 \u00b5s per loop"
]
},
{
@ -346,7 +566,7 @@
"stream": "stdout",
"text": [
"\n",
"1000 loops, best of 3: 247 \u00b5s per loop"
"10000 loops, best of 3: 125 \u00b5s per loop"
]
},
{
@ -354,7 +574,7 @@
"stream": "stdout",
"text": [
"\n",
"1000 loops, best of 3: 209 \u00b5s per loop"
"10000 loops, best of 3: 105 \u00b5s per loop"
]
},
{
@ -364,7 +584,7 @@
"\n",
"\n",
"Results for 1000 integers in range 1-1000\n",
"1000 loops, best of 3: 241 \u00b5s per loop"
"10000 loops, best of 3: 122 \u00b5s per loop"
]
},
{
@ -372,7 +592,7 @@
"stream": "stdout",
"text": [
"\n",
"1000 loops, best of 3: 247 \u00b5s per loop"
"10000 loops, best of 3: 123 \u00b5s per loop"
]
},
{
@ -380,7 +600,7 @@
"stream": "stdout",
"text": [
"\n",
"1000 loops, best of 3: 206 \u00b5s per loop"
"10000 loops, best of 3: 104 \u00b5s per loop"
]
},
{
@ -391,7 +611,7 @@
]
}
],
"prompt_number": 25
"prompt_number": 13
},
{
"cell_type": "markdown",
@ -400,14 +620,6 @@
"### Conclusion\n",
"Interestingly, the `try-except` loop pays off if we have more elements (here: 1000 integers instead of 100) as dictionary keys to check. Also, it doesn't matter much whether the elements exist or do not exist in the dictionary, yet."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}