Skip to content

Instantly share code, notes, and snippets.

@kwinkunks
Last active April 13, 2022 22:48
Show Gist options
  • Save kwinkunks/400a84b98eebe9ac2e670f593dbeb747 to your computer and use it in GitHub Desktop.
Save kwinkunks/400a84b98eebe9ac2e670f593dbeb747 to your computer and use it in GitHub Desktop.
What's the fastest way to concatenate things into a string?
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "e04ff53a",
"metadata": {},
"source": [
"# Concatenating strings\n",
"\n",
"What's the fastest way to join a bunch of numbers together into a big string?"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "f5ab27df",
"metadata": {},
"outputs": [],
"source": [
"def s_concat_naive(n: int) -> str:\n",
" \"\"\"Just loop and append, then join.\"\"\"\n",
" s = []\n",
" for i in range(n):\n",
" s.append(str(i))\n",
" return ''.join(s)\n",
"\n",
"n = 100_000\n",
"s = s_concat_naive(n)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "f8ca2e3d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"17.1 ms ± 176 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"%timeit s_concat_naive(n)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "b382506c",
"metadata": {},
"outputs": [],
"source": [
"from functools import reduce\n",
"from operator import add\n",
"\n",
"def s_concat_functional(n: int) -> str:\n",
" \"\"\"Pure functional.\"\"\"\n",
" return reduce(add, map(str, range(n)))\n",
"\n",
"assert s_concat_functional(n) == s"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "d138a454",
"metadata": {},
"outputs": [],
"source": [
"%timeit s_concat_functional(n)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "0c690f30",
"metadata": {},
"outputs": [],
"source": [
"def s_concat_preallocate(n: int) -> str:\n",
" \"\"\"Preallocate the list.\"\"\"\n",
" s = [None] * n\n",
" for i in range(n):\n",
" s[i] = str(i)\n",
" return ''.join(s)\n",
"\n",
"assert s_concat_preallocate(n) == s"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "51fabe70",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"15.7 ms ± 252 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"%timeit s_concat_preallocate(n)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "fad73596",
"metadata": {},
"outputs": [],
"source": [
"def s_concat_genexpr(n: int) -> str:\n",
" \"\"\"Stringify in a generator expression.\"\"\"\n",
" return ''.join(str(i) for i in range(n))"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "5004e755",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"15.1 ms ± 248 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"%timeit s_concat_genexpr(n)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "668bd49f",
"metadata": {},
"outputs": [],
"source": [
"def s_concat_format(n: int) -> str:\n",
" \"\"\"Make the string, then pass contents via format.\"\"\"\n",
" meta = n * '{}'\n",
" return meta.format(*range(n))\n",
"\n",
"assert s_concat_format(n) == s"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "75ab78ba",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"6.62 ms ± 48.8 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"%timeit s_concat_format(n)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "py39",
"language": "python",
"name": "py39"
},
"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.9.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment