Last active
January 7, 2020 20:44
-
-
Save gwbischof/6efafc15b620166a1492a5a610bc0ebf 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": 7, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "from hashlib import md5\n", | |
| "from dask.utils import Dispatch\n", | |
| "from toolz import identity\n", | |
| "import random\n", | |
| "import string\n", | |
| "\n", | |
| "def tokenize_simplified(descriptor):\n", | |
| " return md5(str(tuple(map(normalize_token, descriptor))).encode()).hexdigest()\n", | |
| "\n", | |
| "def tokenize_dask(*args, **kwargs):\n", | |
| " # Tokenize method from dask\n", | |
| " if kwargs:\n", | |
| " args = args + (kwargs,)\n", | |
| " return md5(str(tuple(map(normalize_token, args))).encode()).hexdigest()\n", | |
| "\n", | |
| "def tokenize_new(*args):\n", | |
| " return md5(str(tuple(map(normalize_token, args))).encode()).hexdigest()\n", | |
| "\n", | |
| "def tokenize_new2(args):\n", | |
| " return md5(str(tuple(map(normalize_token, args))).encode()).hexdigest()\n", | |
| "\n", | |
| "normalize_token = Dispatch()\n", | |
| "normalize_token.register(\n", | |
| " (int, float, str, bytes, type(None), type, slice, complex, type(Ellipsis)), identity\n", | |
| ")\n", | |
| "\n", | |
| "@normalize_token.register(dict)\n", | |
| "def normalize_dict(d):\n", | |
| " return normalize_token(sorted(d.items(), key=str))\n", | |
| "\n", | |
| "@normalize_token.register(set)\n", | |
| "def normalize_set(s):\n", | |
| " return normalize_token(sorted(s, key=str))\n", | |
| "\n", | |
| "\n", | |
| "@normalize_token.register((tuple, list))\n", | |
| "def normalize_seq(seq):\n", | |
| " return type(seq).__name__, list(map(normalize_token, seq))\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "descriptor = {random.choice(string.ascii_letters): \n", | |
| " {random.choice(string.ascii_letters): 1000 * random.choice(string.ascii_letters) for _ in range(1000)} \n", | |
| " for _ in range(1000)}" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "metadata": { | |
| "scrolled": true | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "19.4 µs ± 161 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "%%timeit\n", | |
| "tokenize_simplified(descriptor)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 4, | |
| "metadata": { | |
| "scrolled": true | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "24.2 ms ± 274 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "%%timeit\n", | |
| "tokenize_dask(descriptor)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 5, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "24.2 ms ± 248 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "%%timeit\n", | |
| "tokenize_new(descriptor)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 6, | |
| "metadata": { | |
| "scrolled": true | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "19.4 µs ± 176 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "%%timeit\n", | |
| "tokenize_new2(descriptor)" | |
| ] | |
| } | |
| ], | |
| "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.7.3" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 2 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment