diff --git a/.ipynb_checkpoints/timeit_test-checkpoint.ipynb b/.ipynb_checkpoints/timeit_test-checkpoint.ipynb new file mode 100644 index 0000000..ed84d5c --- /dev/null +++ b/.ipynb_checkpoints/timeit_test-checkpoint.ipynb @@ -0,0 +1,123 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:c78a9ef71605847c90e18bf10abda603339de473905dc928bd17aacd1ae5bee9" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### String formatting - `.format()` vs. `%s`" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import timeit\n", + "\n", + "def test_format():\n", + " return ['{}'.format(i) for i in range(1000000)]\n", + "\n", + "def test_binaryop():\n", + " return ['%s' %i for i in range(1000000)]\n", + "\n", + "%timeit test_format()\n", + "%timeit test_binaryop()\n", + "\n", + "#print('{}: {}\\n{}: {}'.format('format()', format_res, '%s', binaryop_res))\n", + "\n", + "################################\n", + "# On my machine\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": [ + "1 loops, best of 3: 400 ms per loop\n", + "1 loops, best of 3: 241 ms per loop" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### String Reversing - `[::-1]` vs. `''.join(reversed())`" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def reverse_join(my_str):\n", + " return ''.join(reversed(my_str))\n", + " \n", + "def reverse_slizing(my_str):\n", + " return my_str[::-1]\n", + "\n", + "\n", + "# Test to show that both work\n", + "a = reverse_join('abcd')\n", + "b = reverse_slizing('abcd')\n", + "assert(a == b and a == 'dcba')\n", + "\n", + "%timeit reverse_join('abcd')\n", + "%timeit reverse_slizing('abcd')\n", + "\n", + "# Python 3.4.0\n", + "# MacOS X 10.9.2\n", + "# 2.4 GHz Intel Core Duo\n", + "# 8 GB 1067 Mhz DDR3\n", + "#" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "1000000 loops, best of 3: 1.28 \u00b5s per loop\n", + "1000000 loops, best of 3: 337 ns per loop" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n" + ] + } + ], + "prompt_number": 13 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file diff --git a/README.md b/README.md index 6366ca7..0f93200 100755 --- a/README.md +++ b/README.md @@ -2,3 +2,9 @@ python_reference ================ Syntax examples for useful Python functions, methods, and modules + + +###Links to view the IPython Notebooks + +- [Python benchmarks via `timeit`](http://nbviewer.ipython.org/github/rasbt/python_reference/blob/master/timeit_test.ipynb?create=1) + diff --git a/timeit_test.ipynb b/timeit_test.ipynb index d91402d..ed84d5c 100644 --- a/timeit_test.ipynb +++ b/timeit_test.ipynb @@ -1,12 +1,20 @@ { "metadata": { - "name": "" + "name": "", + "signature": "sha256:c78a9ef71605847c90e18bf10abda603339de473905dc928bd17aacd1ae5bee9" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### String formatting - `.format()` vs. `%s`" + ] + }, { "cell_type": "code", "collapsed": false, @@ -32,7 +40,7 @@ "# MacOS X 10.9.2\n", "# 2.5 GHz Intel Core i5\n", "# 4 GB 1600 Mhz DDR3\n", - "#\n" + "#" ], "language": "python", "metadata": {}, @@ -55,13 +63,58 @@ ], "prompt_number": 3 }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### String Reversing - `[::-1]` vs. `''.join(reversed())`" + ] + }, { "cell_type": "code", "collapsed": false, - "input": [], + "input": [ + "def reverse_join(my_str):\n", + " return ''.join(reversed(my_str))\n", + " \n", + "def reverse_slizing(my_str):\n", + " return my_str[::-1]\n", + "\n", + "\n", + "# Test to show that both work\n", + "a = reverse_join('abcd')\n", + "b = reverse_slizing('abcd')\n", + "assert(a == b and a == 'dcba')\n", + "\n", + "%timeit reverse_join('abcd')\n", + "%timeit reverse_slizing('abcd')\n", + "\n", + "# Python 3.4.0\n", + "# MacOS X 10.9.2\n", + "# 2.4 GHz Intel Core Duo\n", + "# 8 GB 1067 Mhz DDR3\n", + "#" + ], "language": "python", "metadata": {}, - "outputs": [] + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "1000000 loops, best of 3: 1.28 \u00b5s per loop\n", + "1000000 loops, best of 3: 337 ns per loop" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n" + ] + } + ], + "prompt_number": 13 } ], "metadata": {}