mirror of
https://github.com/rasbt/python_reference.git
synced 2024-11-24 04:21:15 +00:00
198 lines
5.3 KiB
Plaintext
198 lines
5.3 KiB
Plaintext
{
|
|
"metadata": {
|
|
"name": "",
|
|
"signature": "sha256:3d878b64b4503fd987496df562af53903ad85d4cce103ea0a2e6c456519c03c7"
|
|
},
|
|
"nbformat": 3,
|
|
"nbformat_minor": 0,
|
|
"worksheets": [
|
|
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"[Sebastian Raschka](http://sebastianraschka.com) \n",
|
|
"last updated: 05/03/2014\n",
|
|
"\n",
|
|
"- [Link to this IPython Notebook on GitHub](https://github.com/rasbt/python_reference/blob/master/benchmarks/palindrome_timeit.ipynb) \n",
|
|
"- [Link to the GitHub repository](https://github.com/rasbt/python_reference) \n",
|
|
"\n",
|
|
"<br>\n",
|
|
"<br>"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"#Timing different Implementations of palindrome functions"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"collapsed": false,
|
|
"input": [
|
|
"import re\n",
|
|
"import timeit\n",
|
|
"import string\n",
|
|
"\n",
|
|
"# All functions return True if an input string is a palindrome. Else returns False.\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"####\n",
|
|
"#### case-insensitive ignoring punctuation characters\n",
|
|
"####\n",
|
|
"\n",
|
|
"def palindrome_short(my_str):\n",
|
|
" stripped_str = \"\".join(l.lower() for l in my_str if l.isalpha())\n",
|
|
" return stripped_str == stripped_str[::-1]\n",
|
|
"\n",
|
|
"def palindrome_regex(my_str):\n",
|
|
" return re.sub('\\W', '', my_str.lower()) == re.sub('\\W', '', my_str[::-1].lower())\n",
|
|
"\n",
|
|
"def palindrome_stringlib(my_str):\n",
|
|
" LOWERS = set(string.ascii_lowercase)\n",
|
|
" letters = [c for c in my_str.lower() if c in LOWERS]\n",
|
|
" return letters == letters[::-1]\n",
|
|
"\n",
|
|
"LOWERS = set(string.ascii_lowercase)\n",
|
|
"def palindrome_stringlib2(my_str):\n",
|
|
" letters = [c for c in my_str.lower() if c in LOWERS]\n",
|
|
" return letters == letters[::-1]\n",
|
|
"\n",
|
|
"def palindrome_isalpha(my_str):\n",
|
|
" stripped_str = [l for l in my_str.lower() if l.isalpha()]\n",
|
|
" return stripped_str == stripped_str[::-1]\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"####\n",
|
|
"#### functions considering all characters (case-sensitive)\n",
|
|
"####\n",
|
|
"\n",
|
|
"def palindrome_reverse1(my_str):\n",
|
|
" return my_str == my_str[::-1]\n",
|
|
"\n",
|
|
"def palindrome_reverse2(my_str):\n",
|
|
" return my_str == ''.join(reversed(my_str))\n",
|
|
"\n",
|
|
"def palindrome_recurs(my_str):\n",
|
|
" if len(my_str) < 2:\n",
|
|
" return True\n",
|
|
" if my_str[0] != my_str[-1]:\n",
|
|
" return False\n",
|
|
" return palindrome(my_str[1:-1])\n",
|
|
"\n",
|
|
"\n",
|
|
"\n"
|
|
],
|
|
"language": "python",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"prompt_number": 10
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"collapsed": false,
|
|
"input": [
|
|
"test_str = \"Go hang a salami. I'm a lasagna hog.\"\n",
|
|
"\n",
|
|
"print('case-insensitive functions ignoring punctuation characters')\n",
|
|
"%timeit palindrome_short(test_str)\n",
|
|
"%timeit palindrome_regex(test_str)\n",
|
|
"%timeit palindrome_stringlib(test_str)\n",
|
|
"%timeit palindrome_stringlib2(test_str)\n",
|
|
"%timeit palindrome_isalpha(test_str)\n",
|
|
"\n",
|
|
"print('\\n\\nfunctions considering all characters (case-sensitive)')\n",
|
|
"%timeit palindrome_reverse1(test_str)\n",
|
|
"%timeit palindrome_reverse2(test_str)\n",
|
|
"%timeit palindrome_recurs(test_str)\n"
|
|
],
|
|
"language": "python",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"output_type": "stream",
|
|
"stream": "stdout",
|
|
"text": [
|
|
"case-insensitive functions ignoring punctuation characters\n",
|
|
"100000 loops, best of 3: 15.3 \u00b5s per loop"
|
|
]
|
|
},
|
|
{
|
|
"output_type": "stream",
|
|
"stream": "stdout",
|
|
"text": [
|
|
"\n",
|
|
"100000 loops, best of 3: 19.9 \u00b5s per loop"
|
|
]
|
|
},
|
|
{
|
|
"output_type": "stream",
|
|
"stream": "stdout",
|
|
"text": [
|
|
"\n",
|
|
"100000 loops, best of 3: 13.5 \u00b5s per loop"
|
|
]
|
|
},
|
|
{
|
|
"output_type": "stream",
|
|
"stream": "stdout",
|
|
"text": [
|
|
"\n",
|
|
"100000 loops, best of 3: 8.58 \u00b5s per loop"
|
|
]
|
|
},
|
|
{
|
|
"output_type": "stream",
|
|
"stream": "stdout",
|
|
"text": [
|
|
"\n",
|
|
"100000 loops, best of 3: 9.42 \u00b5s per loop"
|
|
]
|
|
},
|
|
{
|
|
"output_type": "stream",
|
|
"stream": "stdout",
|
|
"text": [
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"functions considering all characters (case-sensitive)\n",
|
|
"1000000 loops, best of 3: 508 ns per loop"
|
|
]
|
|
},
|
|
{
|
|
"output_type": "stream",
|
|
"stream": "stdout",
|
|
"text": [
|
|
"\n",
|
|
"100000 loops, best of 3: 3.08 \u00b5s per loop"
|
|
]
|
|
},
|
|
{
|
|
"output_type": "stream",
|
|
"stream": "stdout",
|
|
"text": [
|
|
"\n",
|
|
"1000000 loops, best of 3: 480 ns per loop"
|
|
]
|
|
},
|
|
{
|
|
"output_type": "stream",
|
|
"stream": "stdout",
|
|
"text": [
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"prompt_number": 11
|
|
}
|
|
],
|
|
"metadata": {}
|
|
}
|
|
]
|
|
} |