Skip to content

Commit 776792f

Browse files
committed
timeit benchmarks
1 parent 47e4aca commit 776792f

3 files changed

Lines changed: 186 additions & 4 deletions

File tree

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
{
2+
"metadata": {
3+
"name": "",
4+
"signature": "sha256:c78a9ef71605847c90e18bf10abda603339de473905dc928bd17aacd1ae5bee9"
5+
},
6+
"nbformat": 3,
7+
"nbformat_minor": 0,
8+
"worksheets": [
9+
{
10+
"cells": [
11+
{
12+
"cell_type": "markdown",
13+
"metadata": {},
14+
"source": [
15+
"### String formatting - `.format()` vs. `%s`"
16+
]
17+
},
18+
{
19+
"cell_type": "code",
20+
"collapsed": false,
21+
"input": [
22+
"import timeit\n",
23+
"\n",
24+
"def test_format():\n",
25+
" return ['{}'.format(i) for i in range(1000000)]\n",
26+
"\n",
27+
"def test_binaryop():\n",
28+
" return ['%s' %i for i in range(1000000)]\n",
29+
"\n",
30+
"%timeit test_format()\n",
31+
"%timeit test_binaryop()\n",
32+
"\n",
33+
"#print('{}: {}\\n{}: {}'.format('format()', format_res, '%s', binaryop_res))\n",
34+
"\n",
35+
"################################\n",
36+
"# On my machine\n",
37+
"################################\n",
38+
"#\n",
39+
"# Python 3.4.0\n",
40+
"# MacOS X 10.9.2\n",
41+
"# 2.5 GHz Intel Core i5\n",
42+
"# 4 GB 1600 Mhz DDR3\n",
43+
"#"
44+
],
45+
"language": "python",
46+
"metadata": {},
47+
"outputs": [
48+
{
49+
"output_type": "stream",
50+
"stream": "stdout",
51+
"text": [
52+
"1 loops, best of 3: 400 ms per loop\n",
53+
"1 loops, best of 3: 241 ms per loop"
54+
]
55+
},
56+
{
57+
"output_type": "stream",
58+
"stream": "stdout",
59+
"text": [
60+
"\n"
61+
]
62+
}
63+
],
64+
"prompt_number": 3
65+
},
66+
{
67+
"cell_type": "markdown",
68+
"metadata": {},
69+
"source": [
70+
"### String Reversing - `[::-1]` vs. `''.join(reversed())`"
71+
]
72+
},
73+
{
74+
"cell_type": "code",
75+
"collapsed": false,
76+
"input": [
77+
"def reverse_join(my_str):\n",
78+
" return ''.join(reversed(my_str))\n",
79+
" \n",
80+
"def reverse_slizing(my_str):\n",
81+
" return my_str[::-1]\n",
82+
"\n",
83+
"\n",
84+
"# Test to show that both work\n",
85+
"a = reverse_join('abcd')\n",
86+
"b = reverse_slizing('abcd')\n",
87+
"assert(a == b and a == 'dcba')\n",
88+
"\n",
89+
"%timeit reverse_join('abcd')\n",
90+
"%timeit reverse_slizing('abcd')\n",
91+
"\n",
92+
"# Python 3.4.0\n",
93+
"# MacOS X 10.9.2\n",
94+
"# 2.4 GHz Intel Core Duo\n",
95+
"# 8 GB 1067 Mhz DDR3\n",
96+
"#"
97+
],
98+
"language": "python",
99+
"metadata": {},
100+
"outputs": [
101+
{
102+
"output_type": "stream",
103+
"stream": "stdout",
104+
"text": [
105+
"1000000 loops, best of 3: 1.28 \u00b5s per loop\n",
106+
"1000000 loops, best of 3: 337 ns per loop"
107+
]
108+
},
109+
{
110+
"output_type": "stream",
111+
"stream": "stdout",
112+
"text": [
113+
"\n"
114+
]
115+
}
116+
],
117+
"prompt_number": 13
118+
}
119+
],
120+
"metadata": {}
121+
}
122+
]
123+
}

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,9 @@ python_reference
22
================
33

44
Syntax examples for useful Python functions, methods, and modules
5+
6+
7+
###Links to view the IPython Notebooks
8+
9+
- [Python benchmarks via `timeit`](http://nbviewer.ipython.org/github/rasbt/python_reference/blob/master/timeit_test.ipynb?create=1)
10+

timeit_test.ipynb

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
{
22
"metadata": {
3-
"name": ""
3+
"name": "",
4+
"signature": "sha256:c78a9ef71605847c90e18bf10abda603339de473905dc928bd17aacd1ae5bee9"
45
},
56
"nbformat": 3,
67
"nbformat_minor": 0,
78
"worksheets": [
89
{
910
"cells": [
11+
{
12+
"cell_type": "markdown",
13+
"metadata": {},
14+
"source": [
15+
"### String formatting - `.format()` vs. `%s`"
16+
]
17+
},
1018
{
1119
"cell_type": "code",
1220
"collapsed": false,
@@ -32,7 +40,7 @@
3240
"# MacOS X 10.9.2\n",
3341
"# 2.5 GHz Intel Core i5\n",
3442
"# 4 GB 1600 Mhz DDR3\n",
35-
"#\n"
43+
"#"
3644
],
3745
"language": "python",
3846
"metadata": {},
@@ -55,13 +63,58 @@
5563
],
5664
"prompt_number": 3
5765
},
66+
{
67+
"cell_type": "markdown",
68+
"metadata": {},
69+
"source": [
70+
"### String Reversing - `[::-1]` vs. `''.join(reversed())`"
71+
]
72+
},
5873
{
5974
"cell_type": "code",
6075
"collapsed": false,
61-
"input": [],
76+
"input": [
77+
"def reverse_join(my_str):\n",
78+
" return ''.join(reversed(my_str))\n",
79+
" \n",
80+
"def reverse_slizing(my_str):\n",
81+
" return my_str[::-1]\n",
82+
"\n",
83+
"\n",
84+
"# Test to show that both work\n",
85+
"a = reverse_join('abcd')\n",
86+
"b = reverse_slizing('abcd')\n",
87+
"assert(a == b and a == 'dcba')\n",
88+
"\n",
89+
"%timeit reverse_join('abcd')\n",
90+
"%timeit reverse_slizing('abcd')\n",
91+
"\n",
92+
"# Python 3.4.0\n",
93+
"# MacOS X 10.9.2\n",
94+
"# 2.4 GHz Intel Core Duo\n",
95+
"# 8 GB 1067 Mhz DDR3\n",
96+
"#"
97+
],
6298
"language": "python",
6399
"metadata": {},
64-
"outputs": []
100+
"outputs": [
101+
{
102+
"output_type": "stream",
103+
"stream": "stdout",
104+
"text": [
105+
"1000000 loops, best of 3: 1.28 \u00b5s per loop\n",
106+
"1000000 loops, best of 3: 337 ns per loop"
107+
]
108+
},
109+
{
110+
"output_type": "stream",
111+
"stream": "stdout",
112+
"text": [
113+
"\n"
114+
]
115+
}
116+
],
117+
"prompt_number": 13
65118
}
66119
],
67120
"metadata": {}

0 commit comments

Comments
 (0)