Skip to content

Commit faa4c1b

Browse files
committed
palindrome timeit
1 parent 42f06f8 commit faa4c1b

1 file changed

Lines changed: 147 additions & 0 deletions

File tree

benchmarks/palindrome_timeit.ipynb

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
{
2+
"metadata": {
3+
"name": "",
4+
"signature": "sha256:842cb7ae4f520376ff820e7bcd278db91a026ac1fa159ca6e8a4b38296bc10f5"
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+
"\n",
27+
"# All functions return True if an input string is a palindrome. Else returns False.\n",
28+
"\n",
29+
"\n",
30+
"\n",
31+
"####\n",
32+
"#### case-insensitive ignoring punctuation characters\n",
33+
"####\n",
34+
"\n",
35+
"def palindrome_short(my_str):\n",
36+
" stripped_str = \"\".join(l.lower() for l in my_str if l.isalpha())\n",
37+
" return stripped_str == stripped_str[::-1]\n",
38+
"\n",
39+
"def palindrome_regex(my_str):\n",
40+
" return re.sub('\\W', '', my_str.lower()) == re.sub('\\W', '', my_str[::-1].lower())\n",
41+
"\n",
42+
"####\n",
43+
"#### functions considering all characters (case-sensitive)\n",
44+
"####\n",
45+
"\n",
46+
"def palindrome_reverse1(my_str):\n",
47+
" return my_str == my_str[::-1]\n",
48+
"\n",
49+
"def palindrome_reverse2(my_str):\n",
50+
" return my_str == ''.join(reversed(my_str))\n",
51+
"\n",
52+
"def palindrome_recurs(my_str):\n",
53+
" if len(my_str) < 2:\n",
54+
" return True\n",
55+
" if my_str[0] != my_str[-1]:\n",
56+
" return False\n",
57+
" return palindrome(my_str[1:-1])\n"
58+
],
59+
"language": "python",
60+
"metadata": {},
61+
"outputs": [],
62+
"prompt_number": 6
63+
},
64+
{
65+
"cell_type": "code",
66+
"collapsed": false,
67+
"input": [
68+
"test_str = \"Go hang a salami. I'm a lasagna hog.\"\n",
69+
"\n",
70+
"print('case-insensitive functions ignoring punctuation characters')\n",
71+
"%timeit palindrome_short(test_str)\n",
72+
"%timeit palindrome_regex(test_str)\n",
73+
"\n",
74+
"print('\\n\\nfunctions considering all characters (case-sensitive)')\n",
75+
"%timeit palindrome_reverse1(test_str)\n",
76+
"%timeit palindrome_reverse2(test_str)\n",
77+
"%timeit palindrome_recurs(test_str)\n"
78+
],
79+
"language": "python",
80+
"metadata": {},
81+
"outputs": [
82+
{
83+
"output_type": "stream",
84+
"stream": "stdout",
85+
"text": [
86+
"case-insensitive functions ignoring punctuation characters\n",
87+
"100000 loops, best of 3: 15.4 \u00b5s per loop"
88+
]
89+
},
90+
{
91+
"output_type": "stream",
92+
"stream": "stdout",
93+
"text": [
94+
"\n",
95+
"10000 loops, best of 3: 20.3 \u00b5s per loop"
96+
]
97+
},
98+
{
99+
"output_type": "stream",
100+
"stream": "stdout",
101+
"text": [
102+
"\n",
103+
"\n",
104+
"\n",
105+
"functions considering all characters (case-sensitive)\n",
106+
"1000000 loops, best of 3: 508 ns per loop"
107+
]
108+
},
109+
{
110+
"output_type": "stream",
111+
"stream": "stdout",
112+
"text": [
113+
"\n",
114+
"100000 loops, best of 3: 3 \u00b5s per loop"
115+
]
116+
},
117+
{
118+
"output_type": "stream",
119+
"stream": "stdout",
120+
"text": [
121+
"\n",
122+
"1000000 loops, best of 3: 509 ns per loop"
123+
]
124+
},
125+
{
126+
"output_type": "stream",
127+
"stream": "stdout",
128+
"text": [
129+
"\n"
130+
]
131+
}
132+
],
133+
"prompt_number": 7
134+
},
135+
{
136+
"cell_type": "code",
137+
"collapsed": false,
138+
"input": [],
139+
"language": "python",
140+
"metadata": {},
141+
"outputs": []
142+
}
143+
],
144+
"metadata": {}
145+
}
146+
]
147+
}

0 commit comments

Comments
 (0)