mirror of
https://github.com/rasbt/python_reference.git
synced 2024-11-24 12:31:14 +00:00
203 lines
4.5 KiB
Plaintext
203 lines
4.5 KiB
Plaintext
{
|
|
"metadata": {
|
|
"name": "",
|
|
"signature": "sha256:30bb811f79dc78fe63221c8960bb4237c811b94fdf197b6479709aff3244cb08"
|
|
},
|
|
"nbformat": 3,
|
|
"nbformat_minor": 0,
|
|
"worksheets": [
|
|
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Python benchmarks via `timeit`"
|
|
]
|
|
},
|
|
{
|
|
"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": [
|
|
"import timeit\n",
|
|
"\n",
|
|
"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
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### List Reversing - `[::-1]` vs. `reverse()` vs. `reversed()`"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"collapsed": false,
|
|
"input": [
|
|
"import timeit\n",
|
|
"\n",
|
|
"def reverse_func(my_list):\n",
|
|
" new_list = my_list[:]\n",
|
|
" new_list.reverse()\n",
|
|
" return new_list\n",
|
|
" \n",
|
|
"def reversed_func(my_list):\n",
|
|
" return list(reversed(my_list))\n",
|
|
"\n",
|
|
"def reverse_slizing(my_list):\n",
|
|
" return my_list[::-1]\n",
|
|
"\n",
|
|
"%timeit reverse_func([1,2,3,4,5])\n",
|
|
"%timeit reversed_func([1,2,3,4,5])\n",
|
|
"%timeit reverse_slizing([1,2,3,4,5])\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: 857 ns per loop\n",
|
|
"1000000 loops, best of 3: 1.78 \u00b5s per loop"
|
|
]
|
|
},
|
|
{
|
|
"output_type": "stream",
|
|
"stream": "stdout",
|
|
"text": [
|
|
"\n",
|
|
"1000000 loops, best of 3: 758 ns per loop"
|
|
]
|
|
},
|
|
{
|
|
"output_type": "stream",
|
|
"stream": "stdout",
|
|
"text": [
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"prompt_number": 10
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"collapsed": false,
|
|
"input": [],
|
|
"language": "python",
|
|
"metadata": {},
|
|
"outputs": []
|
|
}
|
|
],
|
|
"metadata": {}
|
|
}
|
|
]
|
|
} |