Last active
October 22, 2018 02:22
-
-
Save notwa/11fa803ac23f2c389dc8027e7c75c740 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"435 µs ± 4.32 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n", | |
"64.5 µs ± 1.53 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n", | |
"35.8 µs ± 159 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n", | |
"44.4 µs ± 303 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n", | |
"564 µs ± 1.7 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n", | |
"940 µs ± 1.67 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n", | |
"26.4 ms ± 275 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)\n" | |
] | |
} | |
], | |
"source": [ | |
"# only tested on python 3.6\n", | |
"from functools import reduce\n", | |
"\n", | |
"with open(\"ordered set.ipynb\") as f:\n", | |
" s = f.read()\n", | |
"\n", | |
"def naive(s):\n", | |
" d = []\n", | |
" for c in s:\n", | |
" if c not in d:\n", | |
" d.append(c)\n", | |
" return d\n", | |
"\n", | |
"%timeit naive(s)\n", | |
"%timeit list(dict(zip(s, s)))\n", | |
"%timeit list(dict.fromkeys(s))\n", | |
"%timeit sorted(set(s), key=s.index)\n", | |
"%timeit reduce(lambda x, y: x if y in x else x + [y], s, [])\n", | |
"%timeit reduce(lambda x, y: x + [y] * (y not in x), s, [])\n", | |
"%timeit [c[1] for c in enumerate(s) if list(s).index(c[1]) == c[0]]" | |
] | |
} | |
], | |
"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