Skip to content

Commit a333cd0

Browse files
committed
palindrom upd
1 parent 2152282 commit a333cd0

2 files changed

Lines changed: 191 additions & 8 deletions

File tree

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
{
2+
"metadata": {
3+
"name": "",
4+
"signature": "sha256:710150d6bf8fe09c3a2348c765457cdea364ebe260d10f57be9349b32ef3ff50"
5+
},
6+
"nbformat": 3,
7+
"nbformat_minor": 0,
8+
"worksheets": [
9+
{
10+
"cells": [
11+
{
12+
"cell_type": "markdown",
13+
"metadata": {},
14+
"source": [
15+
"Sebastian Raschka 04/2014\n",
16+
"\n",
17+
"#Timing different Implementations of palindrome functions"
18+
]
19+
},
20+
{
21+
"cell_type": "code",
22+
"collapsed": false,
23+
"input": [
24+
"import re\n",
25+
"import timeit\n",
26+
"import string\n",
27+
"\n",
28+
"# All functions return True if an input string is a palindrome. Else returns False.\n",
29+
"\n",
30+
"\n",
31+
"\n",
32+
"####\n",
33+
"#### case-insensitive ignoring punctuation characters\n",
34+
"####\n",
35+
"\n",
36+
"def palindrome_short(my_str):\n",
37+
" stripped_str = \"\".join(l.lower() for l in my_str if l.isalpha())\n",
38+
" return stripped_str == stripped_str[::-1]\n",
39+
"\n",
40+
"def palindrome_regex(my_str):\n",
41+
" return re.sub('\\W', '', my_str.lower()) == re.sub('\\W', '', my_str[::-1].lower())\n",
42+
"\n",
43+
"def palindrome_stringlib(my_str):\n",
44+
" LOWERS = set(string.ascii_lowercase)\n",
45+
" letters = [c for c in my_str.lower() if c in LOWERS]\n",
46+
" return letters == letters[::-1]\n",
47+
"\n",
48+
"####\n",
49+
"#### functions considering all characters (case-sensitive)\n",
50+
"####\n",
51+
"\n",
52+
"def palindrome_reverse1(my_str):\n",
53+
" return my_str == my_str[::-1]\n",
54+
"\n",
55+
"def palindrome_reverse2(my_str):\n",
56+
" return my_str == ''.join(reversed(my_str))\n",
57+
"\n",
58+
"def palindrome_recurs(my_str):\n",
59+
" if len(my_str) < 2:\n",
60+
" return True\n",
61+
" if my_str[0] != my_str[-1]:\n",
62+
" return False\n",
63+
" return palindrome(my_str[1:-1])\n",
64+
"\n",
65+
"\n",
66+
"\n"
67+
],
68+
"language": "python",
69+
"metadata": {},
70+
"outputs": [],
71+
"prompt_number": 1
72+
},
73+
{
74+
"cell_type": "code",
75+
"collapsed": false,
76+
"input": [
77+
"test_str = \"Go hang a salami. I'm a lasagna hog.\"\n",
78+
"\n",
79+
"print('case-insensitive functions ignoring punctuation characters')\n",
80+
"%timeit palindrome_short(test_str)\n",
81+
"%timeit palindrome_regex(test_str)\n",
82+
"%timeit palindrome_stringlib(test_str)\n",
83+
"\n",
84+
"print('\\n\\nfunctions considering all characters (case-sensitive)')\n",
85+
"%timeit palindrome_reverse1(test_str)\n",
86+
"%timeit palindrome_reverse2(test_str)\n",
87+
"%timeit palindrome_recurs(test_str)\n"
88+
],
89+
"language": "python",
90+
"metadata": {},
91+
"outputs": [
92+
{
93+
"output_type": "stream",
94+
"stream": "stdout",
95+
"text": [
96+
"case-insensitive functions ignoring punctuation characters\n",
97+
"100000 loops, best of 3: 15.4 \u00b5s per loop"
98+
]
99+
},
100+
{
101+
"output_type": "stream",
102+
"stream": "stdout",
103+
"text": [
104+
"\n",
105+
"10000 loops, best of 3: 20.1 \u00b5s per loop"
106+
]
107+
},
108+
{
109+
"output_type": "stream",
110+
"stream": "stdout",
111+
"text": [
112+
"\n",
113+
"100000 loops, best of 3: 12.3 \u00b5s per loop"
114+
]
115+
},
116+
{
117+
"output_type": "stream",
118+
"stream": "stdout",
119+
"text": [
120+
"\n",
121+
"\n",
122+
"\n",
123+
"functions considering all characters (case-sensitive)\n",
124+
"1000000 loops, best of 3: 500 ns per loop"
125+
]
126+
},
127+
{
128+
"output_type": "stream",
129+
"stream": "stdout",
130+
"text": [
131+
"\n",
132+
"100000 loops, best of 3: 2.93 \u00b5s per loop"
133+
]
134+
},
135+
{
136+
"output_type": "stream",
137+
"stream": "stdout",
138+
"text": [
139+
"\n",
140+
"1000000 loops, best of 3: 505 ns per loop"
141+
]
142+
},
143+
{
144+
"output_type": "stream",
145+
"stream": "stdout",
146+
"text": [
147+
"\n"
148+
]
149+
}
150+
],
151+
"prompt_number": 2
152+
},
153+
{
154+
"cell_type": "code",
155+
"collapsed": false,
156+
"input": [],
157+
"language": "python",
158+
"metadata": {},
159+
"outputs": []
160+
}
161+
],
162+
"metadata": {}
163+
}
164+
]
165+
}

benchmarks/palindrome_timeit.ipynb

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"metadata": {
33
"name": "",
4-
"signature": "sha256:842cb7ae4f520376ff820e7bcd278db91a026ac1fa159ca6e8a4b38296bc10f5"
4+
"signature": "sha256:710150d6bf8fe09c3a2348c765457cdea364ebe260d10f57be9349b32ef3ff50"
55
},
66
"nbformat": 3,
77
"nbformat_minor": 0,
@@ -23,6 +23,7 @@
2323
"input": [
2424
"import re\n",
2525
"import timeit\n",
26+
"import string\n",
2627
"\n",
2728
"# All functions return True if an input string is a palindrome. Else returns False.\n",
2829
"\n",
@@ -39,6 +40,11 @@
3940
"def palindrome_regex(my_str):\n",
4041
" return re.sub('\\W', '', my_str.lower()) == re.sub('\\W', '', my_str[::-1].lower())\n",
4142
"\n",
43+
"def palindrome_stringlib(my_str):\n",
44+
" LOWERS = set(string.ascii_lowercase)\n",
45+
" letters = [c for c in my_str.lower() if c in LOWERS]\n",
46+
" return letters == letters[::-1]\n",
47+
"\n",
4248
"####\n",
4349
"#### functions considering all characters (case-sensitive)\n",
4450
"####\n",
@@ -54,12 +60,15 @@
5460
" return True\n",
5561
" if my_str[0] != my_str[-1]:\n",
5662
" return False\n",
57-
" return palindrome(my_str[1:-1])\n"
63+
" return palindrome(my_str[1:-1])\n",
64+
"\n",
65+
"\n",
66+
"\n"
5867
],
5968
"language": "python",
6069
"metadata": {},
6170
"outputs": [],
62-
"prompt_number": 6
71+
"prompt_number": 1
6372
},
6473
{
6574
"cell_type": "code",
@@ -70,6 +79,7 @@
7079
"print('case-insensitive functions ignoring punctuation characters')\n",
7180
"%timeit palindrome_short(test_str)\n",
7281
"%timeit palindrome_regex(test_str)\n",
82+
"%timeit palindrome_stringlib(test_str)\n",
7383
"\n",
7484
"print('\\n\\nfunctions considering all characters (case-sensitive)')\n",
7585
"%timeit palindrome_reverse1(test_str)\n",
@@ -92,7 +102,15 @@
92102
"stream": "stdout",
93103
"text": [
94104
"\n",
95-
"10000 loops, best of 3: 20.3 \u00b5s per loop"
105+
"10000 loops, best of 3: 20.1 \u00b5s per loop"
106+
]
107+
},
108+
{
109+
"output_type": "stream",
110+
"stream": "stdout",
111+
"text": [
112+
"\n",
113+
"100000 loops, best of 3: 12.3 \u00b5s per loop"
96114
]
97115
},
98116
{
@@ -103,23 +121,23 @@
103121
"\n",
104122
"\n",
105123
"functions considering all characters (case-sensitive)\n",
106-
"1000000 loops, best of 3: 508 ns per loop"
124+
"1000000 loops, best of 3: 500 ns per loop"
107125
]
108126
},
109127
{
110128
"output_type": "stream",
111129
"stream": "stdout",
112130
"text": [
113131
"\n",
114-
"100000 loops, best of 3: 3 \u00b5s per loop"
132+
"100000 loops, best of 3: 2.93 \u00b5s per loop"
115133
]
116134
},
117135
{
118136
"output_type": "stream",
119137
"stream": "stdout",
120138
"text": [
121139
"\n",
122-
"1000000 loops, best of 3: 509 ns per loop"
140+
"1000000 loops, best of 3: 505 ns per loop"
123141
]
124142
},
125143
{
@@ -130,7 +148,7 @@
130148
]
131149
}
132150
],
133-
"prompt_number": 7
151+
"prompt_number": 2
134152
},
135153
{
136154
"cell_type": "code",

0 commit comments

Comments
 (0)