Skip to content

Instantly share code, notes, and snippets.

@travishen
Last active July 8, 2018 10:13
Show Gist options
  • Save travishen/7624f16ea660437d2f1e793e7bc942b4 to your computer and use it in GitHub Desktop.
Save travishen/7624f16ea660437d2f1e793e7bc942b4 to your computer and use it in GitHub Desktop.
cpython interning
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Interning range [-5, 256]"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = 10\n",
"b = 10\n",
"\n",
"a is b"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c = 500\n",
"d = 500\n",
"c is d"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = 10\n",
"b = int(10)\n",
"c = int('10')\n",
"d = int('1010', 2)\n",
"\n",
"a is b is c is d"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### string interning"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = 'helloworld'\n",
"b = 'helloworld'\n",
"\n",
"a is b"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = 'helloworld!'\n",
"b = 'helloworld!'\n",
"\n",
"a is b"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = 'hello world'\n",
"b = 'hello world'\n",
"\n",
"a is b"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"### you can force strings to be interned by sys.intern\n",
"import sys\n",
"\n",
"a = sys.intern('hello world!')\n",
"b = sys.intern('hello world!')\n",
"\n",
"a is b"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"def compare_without_interning(n):\n",
" a = 'a long string that is not interned' * 1000\n",
" b = 'a long string that is not interned' * 1000\n",
" for i in range(n):\n",
" if a == b:\n",
" pass\n",
"def compare_with_interning(n):\n",
" a = sys.intern('a long string that is interned' * 1000)\n",
" b = sys.intern('a long string that is interned' * 1000)\n",
" for i in range(n):\n",
" if a is b:\n",
" pass"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"comparison took: 15.186233284010086\n"
]
}
],
"source": [
"import time\n",
"\n",
"start = time.perf_counter()\n",
"compare_without_interning(10000000)\n",
"end = time.perf_counter()\n",
"print('comparison took:', end - start)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"comparison took: 0.28596086699690204\n"
]
}
],
"source": [
"import time\n",
"\n",
"start = time.perf_counter()\n",
"compare_with_interning(10000000)\n",
"end = time.perf_counter()\n",
"print('comparison 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