Skip to content

Instantly share code, notes, and snippets.

@travishen
Created August 14, 2018 01:49
Show Gist options
  • Select an option

  • Save travishen/ff823ba97d5a6200aef0093a2cb7fbe5 to your computer and use it in GitHub Desktop.

Select an option

Save travishen/ff823ba97d5a6200aef0093a2cb7fbe5 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### pre-calculate"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"def consts():\n",
" a = 24 * 60 # constant\n",
" b = (1, 2, 3) * 3 # tuple are constant\n",
" c = 'abc'* 3 # length < 20\n",
" d = 'overflow' * 10 # length >= 20\n",
" e = [1, 2, 3] * 3 # list are not constant"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(None,\n",
" 24,\n",
" 60,\n",
" 1,\n",
" 2,\n",
" 3,\n",
" 'abc',\n",
" 'overflow',\n",
" 10,\n",
" 1440,\n",
" (1, 2, 3),\n",
" (1, 2, 3, 1, 2, 3, 1, 2, 3),\n",
" 'abcabcabc')"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"consts.__code__.co_consts"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### sequence membership"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(None, 1, 2, 3, 4, 5, (1, 2, 3, 4, 5))"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def const_list_lookup(e):\n",
" return e in [1, 2, 3, 4, 5]\n",
"const_list_lookup.__code__.co_consts"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(None,\n",
" <code object <listcomp> at 0x7f8e3936a420, file \"<ipython-input-2-44d86508875c>\", line 2>,\n",
" 'non_const_seq_lookup.<locals>.<listcomp>',\n",
" 100)"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def non_const_list_lookup(e):\n",
" return e in [i for i in range(100)]\n",
"non_const_list_lookup.__code__.co_consts"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(None, 1, 2, 3, 4, 5, frozenset({1, 2, 3, 4, 5}))"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def const_dict_lookup(e):\n",
" return e in {1, 2, 3, 4, 5}\n",
"const_dict_lookup.__code__.co_consts"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### list, tuple, set lookup comparison"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"import time\n",
"import random"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"def membership_test(e, seq):\n",
" for i in range(1000000):\n",
" if e in seq:\n",
" pass"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"list lookup took: 7.701791076000063\n"
]
}
],
"source": [
"start = time.perf_counter()\n",
"\n",
"lst = [int(1000*random.random()) for i in range(10000)]\n",
"membership_test(55, lst)\n",
"\n",
"end = time.perf_counter()\n",
"\n",
"print('list lookup took:', end - start)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tuple lookup took: 7.798407105000024\n"
]
}
],
"source": [
"start = time.perf_counter()\n",
"\n",
"tpl = (int(1000*random.random()) for i in range(10000))\n",
"membership_test(55, lst)\n",
"\n",
"end = time.perf_counter()\n",
"\n",
"print('tuple lookup took:', end - start)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"set lookup took: 0.06725922400028139\n"
]
}
],
"source": [
"start = time.perf_counter()\n",
"\n",
"st = {int(1000*random.random()) for i in range(10000)}\n",
"membership_test(55, st)\n",
"\n",
"end = time.perf_counter()\n",
"\n",
"print('set lookup took:', end - start)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment