Skip to content

Commit 409d953

Browse files
committed
defaultdict
1 parent eb35644 commit 409d953

2 files changed

Lines changed: 172 additions & 108 deletions

File tree

.ipynb_checkpoints/timeit_tests-checkpoint.ipynb

Lines changed: 86 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"metadata": {
33
"name": "",
4-
"signature": "sha256:d5895f75b2ac58db150d7b521682366a447ffb2fb0b7db7e551edd40e6d1ab10"
4+
"signature": "sha256:8dc4f91bc6a88e15ab0d25fac35b9a7645a7149b5ab4e1e15b2b372362e82ae2"
55
},
66
"nbformat": 3,
77
"nbformat_minor": 0,
@@ -855,63 +855,64 @@
855855
"collapsed": false,
856856
"input": [
857857
"import random\n",
858-
"import copy\n",
859858
"import timeit\n",
859+
"from collections import defaultdict\n",
860860
"\n",
861861
"\n",
862-
"\n",
863-
"def add_element_check1(my_dict, elements):\n",
862+
"def add_element_check1(elements):\n",
863+
" d = dict()\n",
864864
" for e in elements:\n",
865-
" if e not in my_dict:\n",
866-
" my_dict[e] = 1\n",
865+
" if e not in d:\n",
866+
" d[e] = 1\n",
867867
" else:\n",
868-
" my_dict[e] += 1\n",
868+
" d[e] += 1\n",
869+
" return d\n",
869870
" \n",
870-
"def add_element_check2(my_dict, elements):\n",
871+
"def add_element_check2(elements):\n",
872+
" d = dict()\n",
871873
" for e in elements:\n",
872-
" if e not in my_dict:\n",
873-
" my_dict[e] = 0\n",
874-
" my_dict[e] += 1 \n",
875-
"\n",
876-
"def add_element_except(my_dict, elements):\n",
874+
" if e not in d:\n",
875+
" d[e] = 0\n",
876+
" d[e] += 1 \n",
877+
" return d\n",
878+
" \n",
879+
"def add_element_except(elements):\n",
880+
" d = dict()\n",
877881
" for e in elements:\n",
878882
" try:\n",
879-
" my_dict[e] += 1\n",
883+
" d[e] += 1\n",
880884
" except KeyError:\n",
881-
" my_dict[e] = 1\n",
885+
" d[e] = 1\n",
886+
" return d\n",
882887
" \n",
888+
"def add_element_defaultdict(elements):\n",
889+
" d = defaultdict(int)\n",
890+
" for e in elements:\n",
891+
" d[e] += 1\n",
892+
" return d\n",
883893
"\n",
884894
"random.seed(123)\n",
885-
"rand_ints = [random.randrange(1, 10) for i in range(100)]\n",
886-
"empty_dict = {}\n",
887895
"\n",
888896
"print('Results for 100 integers in range 1-10') \n",
889-
"%timeit add_element_check1(copy.deepcopy(empty_dict), rand_ints)\n",
890-
"%timeit add_element_check2(copy.deepcopy(empty_dict), rand_ints)\n",
891-
"%timeit add_element_except(copy.deepcopy(empty_dict), rand_ints)\n",
892-
" \n",
893-
"print('\\nResults for 1000 integers in range 1-10') \n",
894-
"rand_ints = [random.randrange(1, 10) for i in range(1000)]\n",
895-
"empty_dict = {}\n",
896-
"\n",
897-
"%timeit add_element_check1(copy.deepcopy(empty_dict), rand_ints)\n",
898-
"%timeit add_element_check2(copy.deepcopy(empty_dict), rand_ints)\n",
899-
"%timeit add_element_except(copy.deepcopy(empty_dict), rand_ints)\n",
897+
"rand_ints = [random.randrange(1, 10) for i in range(100)]\n",
898+
"%timeit add_element_check1(rand_ints)\n",
899+
"%timeit add_element_check2(rand_ints)\n",
900+
"%timeit add_element_except(rand_ints)\n",
901+
"%timeit add_element_defaultdict(rand_ints)\n",
902+
"\n",
903+
"print('\\nResults for 1000 integers in range 1-5') \n",
904+
"rand_ints = [random.randrange(1, 5) for i in range(1000)]\n",
905+
"%timeit add_element_check1(rand_ints)\n",
906+
"%timeit add_element_check2(rand_ints)\n",
907+
"%timeit add_element_except(rand_ints)\n",
908+
"%timeit add_element_defaultdict(rand_ints)\n",
900909
"\n",
901910
"print('\\nResults for 1000 integers in range 1-1000') \n",
902-
"rand_ints = [random.randrange(1, 10) for i in range(1000)]\n",
903-
"empty_dict = {}\n",
904-
"\n",
905-
"%timeit add_element_check1(copy.deepcopy(empty_dict), rand_ints)\n",
906-
"%timeit add_element_check2(copy.deepcopy(empty_dict), rand_ints)\n",
907-
"%timeit add_element_except(copy.deepcopy(empty_dict), rand_ints)\n",
908-
"\n",
909-
"#\n",
910-
"# Python 3.4.0\n",
911-
"# MacOS X 10.9.2\n",
912-
"# 2.5 GHz Intel Core i5\n",
913-
"# 4 GB 1600 Mhz DDR3\n",
914-
"#"
911+
"rand_ints = [random.randrange(1, 1000) for i in range(1000)]\n",
912+
"%timeit add_element_check1(rand_ints)\n",
913+
"%timeit add_element_check2(rand_ints)\n",
914+
"%timeit add_element_except(rand_ints)\n",
915+
"%timeit add_element_defaultdict(rand_ints)"
915916
],
916917
"language": "python",
917918
"metadata": {},
@@ -921,23 +922,31 @@
921922
"stream": "stdout",
922923
"text": [
923924
"Results for 100 integers in range 1-10\n",
924-
"100000 loops, best of 3: 16.6 \u00b5s per loop"
925+
"10000 loops, best of 3: 24.6 \u00b5s per loop"
926+
]
927+
},
928+
{
929+
"output_type": "stream",
930+
"stream": "stdout",
931+
"text": [
932+
"\n",
933+
"10000 loops, best of 3: 26.2 \u00b5s per loop"
925934
]
926935
},
927936
{
928937
"output_type": "stream",
929938
"stream": "stdout",
930939
"text": [
931940
"\n",
932-
"100000 loops, best of 3: 17.6 \u00b5s per loop"
941+
"10000 loops, best of 3: 25.4 \u00b5s per loop"
933942
]
934943
},
935944
{
936945
"output_type": "stream",
937946
"stream": "stdout",
938947
"text": [
939948
"\n",
940-
"100000 loops, best of 3: 17.9 \u00b5s per loop"
949+
"10000 loops, best of 3: 23 \u00b5s per loop"
941950
]
942951
},
943952
{
@@ -946,24 +955,32 @@
946955
"text": [
947956
"\n",
948957
"\n",
949-
"Results for 1000 integers in range 1-10\n",
950-
"10000 loops, best of 3: 135 \u00b5s per loop"
958+
"Results for 1000 integers in range 1-5\n",
959+
"1000 loops, best of 3: 236 \u00b5s per loop"
951960
]
952961
},
953962
{
954963
"output_type": "stream",
955964
"stream": "stdout",
956965
"text": [
957966
"\n",
958-
"10000 loops, best of 3: 125 \u00b5s per loop"
967+
"1000 loops, best of 3: 235 \u00b5s per loop"
959968
]
960969
},
961970
{
962971
"output_type": "stream",
963972
"stream": "stdout",
964973
"text": [
965974
"\n",
966-
"10000 loops, best of 3: 105 \u00b5s per loop"
975+
"1000 loops, best of 3: 207 \u00b5s per loop"
976+
]
977+
},
978+
{
979+
"output_type": "stream",
980+
"stream": "stdout",
981+
"text": [
982+
"\n",
983+
"10000 loops, best of 3: 177 \u00b5s per loop"
967984
]
968985
},
969986
{
@@ -973,23 +990,31 @@
973990
"\n",
974991
"\n",
975992
"Results for 1000 integers in range 1-1000\n",
976-
"10000 loops, best of 3: 122 \u00b5s per loop"
993+
"1000 loops, best of 3: 268 \u00b5s per loop"
994+
]
995+
},
996+
{
997+
"output_type": "stream",
998+
"stream": "stdout",
999+
"text": [
1000+
"\n",
1001+
"1000 loops, best of 3: 377 \u00b5s per loop"
9771002
]
9781003
},
9791004
{
9801005
"output_type": "stream",
9811006
"stream": "stdout",
9821007
"text": [
9831008
"\n",
984-
"10000 loops, best of 3: 123 \u00b5s per loop"
1009+
"1000 loops, best of 3: 511 \u00b5s per loop"
9851010
]
9861011
},
9871012
{
9881013
"output_type": "stream",
9891014
"stream": "stdout",
9901015
"text": [
9911016
"\n",
992-
"10000 loops, best of 3: 104 \u00b5s per loop"
1017+
"1000 loops, best of 3: 410 \u00b5s per loop"
9931018
]
9941019
},
9951020
{
@@ -1000,14 +1025,21 @@
10001025
]
10011026
}
10021027
],
1003-
"prompt_number": 13
1028+
"prompt_number": 16
1029+
},
1030+
{
1031+
"cell_type": "markdown",
1032+
"metadata": {},
1033+
"source": [
1034+
"### Conclusion"
1035+
]
10041036
},
10051037
{
10061038
"cell_type": "markdown",
10071039
"metadata": {},
10081040
"source": [
1009-
"### Conclusion\n",
1010-
"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."
1041+
"We see from the results that the `try-except` variant is faster than then the `if element in my_dict` alternative if we have a low number of unique elements (here: 1000 integers in the range 1-5), which makes sense: the `except`-block is skipped if an element is already added as a key to the dictionary. However, in this case the `collections.defaultdict` has even a better performance. \n",
1042+
"However, if we are having a relative large number of unique entries(here: 1000 integers in range 1-1000), the `if element in my_dict` approach outperforms the alternative approaches."
10111043
]
10121044
},
10131045
{

0 commit comments

Comments
 (0)