Skip to content

Instantly share code, notes, and snippets.

@hideaki-t
Created August 20, 2017 13:33
Show Gist options
  • Save hideaki-t/976d9d25de343b78a65235f030c48de2 to your computer and use it in GitHub Desktop.
Save hideaki-t/976d9d25de343b78a65235f030c48de2 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"import re\n",
"\n",
"a = ' '.join(str(x) for x in range(4 * 10))\n",
"\n",
"f_pt = re.compile(r'\\d+ \\d+ \\d+ \\d+')\n",
"g_pt = re.compile(r'(\\d+) (\\d+) (\\d+) (\\d+)')\n",
"\n",
"def f(a):\n",
" return [[int(i) for i in x[0].split(' ')] for x in f_pt.finditer(a)]\n",
"\n",
"def g(a):\n",
" return [[int(i) for i in x.groups()] for x in g_pt.finditer(a)]\n",
"\n",
"def h(a):\n",
" def _(a):\n",
" while a:\n",
" w, x, y, z, *a = a\n",
" yield w, x, y, z\n",
" return list(_(a.split(' ')))\n",
"\n",
"def h_(a):\n",
" while a:\n",
" w, x, y, z, *a = a\n",
" yield w, x, y, z\n",
"\n",
"def h2(a, _):\n",
" return list(_(a.split(' ')))\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"19.9 µs ± 285 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n"
]
}
],
"source": [
"%timeit f(a)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"21.6 µs ± 1.54 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n"
]
}
],
"source": [
"%timeit g(a)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5.79 µs ± 344 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n"
]
}
],
"source": [
"%timeit h(a)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5.92 µs ± 266 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n"
]
}
],
"source": [
"%timeit h2(a, h_)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment