diff --git a/benchmarks/timeit_tests.ipynb b/benchmarks/timeit_tests.ipynb
index a183039..4f4ac54 100644
--- a/benchmarks/timeit_tests.ipynb
+++ b/benchmarks/timeit_tests.ipynb
@@ -1,7 +1,7 @@
{
"metadata": {
"name": "",
- "signature": "sha256:57df5708c2fce146df742136edf2eebb38c32146ab9e612bfd303a086b5bf7d9"
+ "signature": "sha256:5f459d4c7e1521c71838da53736d2fbffd43b39d57d8b653cde1ec9333346574"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -18,6 +18,17 @@
"[Link to this IPython Notebook on GitHub](https://github.com/rasbt/python_reference/blob/master/benchmarks/timeit_tests.ipynb)"
]
},
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "
\n",
+ "I am really looking forward to your comments and suggestions to improve and extend this collection! Just send me a quick note \n",
+ "via Twitter: [@rasbt](https://twitter.com/rasbt) \n",
+ "or Email: [bluewoodtree@gmail.com](mailto:bluewoodtree@gmail.com)\n",
+ "
"
+ ]
+ },
{
"cell_type": "markdown",
"metadata": {},
@@ -37,6 +48,8 @@
" - [String reversing: [::-1] vs. `''.join(reversed())`](#str_reverse)\n",
" - [String concatenation: `+=` vs. `''.join()`](#string_concat)\n",
" - [Assembling strings](#string_assembly) \n",
+ " - [Testing if a string is an integer](#is_integer)\n",
+ " - [Testing if a string is a number](#is_number)\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",
@@ -229,6 +242,8 @@
"cell_type": "markdown",
"metadata": {},
"source": [
+ "
\n",
+ "
\n",
"\n",
"## Assembling strings\n",
"\n",
@@ -294,6 +309,161 @@
"cell_type": "markdown",
"metadata": {},
"source": [
+ "
\n",
+ "
\n",
+ "\n",
+ "## Testing if a string is an integer"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import timeit\n",
+ "\n",
+ "def string_is_int(a_str):\n",
+ " try:\n",
+ " int(a_str)\n",
+ " return True\n",
+ " except ValueError:\n",
+ " return False\n",
+ "\n",
+ "an_int = '123'\n",
+ "no_int = '123abc'\n",
+ "\n",
+ "%timeit string_is_int(an_int)\n",
+ "%timeit string_is_int(no_int)\n",
+ "%timeit an_int.isdigit()\n",
+ "%timeit no_int.isdigit()\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: 401 ns per loop\n",
+ "100000 loops, best of 3: 3.04 \u00b5s per loop"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "10000000 loops, best of 3: 92.1 ns per loop"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "10000000 loops, best of 3: 96.3 ns per loop"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "
\n",
+ "
\n",
+ "\n",
+ "## Testing if a string is a number"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "import timeit\n",
+ "\n",
+ "def string_is_number(a_str):\n",
+ " try:\n",
+ " float(a_str)\n",
+ " return True\n",
+ " except ValueError:\n",
+ " return False\n",
+ " \n",
+ "a_float = '1.234'\n",
+ "no_float = '123abc'\n",
+ "\n",
+ "a_float.replace('.','',1).isdigit()\n",
+ "no_float.replace('.','',1).isdigit()\n",
+ "\n",
+ "%timeit string_is_number(an_int)\n",
+ "%timeit string_is_number(no_int)\n",
+ "%timeit a_float.replace('.','',1).isdigit()\n",
+ "%timeit no_float.replace('.','',1).isdigit()\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: 400 ns per loop\n",
+ "1000000 loops, best of 3: 1.15 \u00b5s per loop"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "1000000 loops, best of 3: 452 ns per loop"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n",
+ "1000000 loops, best of 3: 394 ns per loop"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "\n"
+ ]
+ }
+ ],
+ "prompt_number": 6
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "
\n",
+ "
\n",
"\n",
"# List operations"
]
@@ -620,6 +790,14 @@
"### 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": {}