Skip to content

Instantly share code, notes, and snippets.

@ogrisel
Created August 3, 2021 08:56
Show Gist options
  • Save ogrisel/6485c94d47edabab9447ce5660d4d860 to your computer and use it in GitHub Desktop.
Save ogrisel/6485c94d47edabab9447ce5660d4d860 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"source": [
"from joblib import cpu_count, parallel_backend\n",
"\n",
"\n",
"print(f\"physical cores: {cpu_count(only_physical_cores=True)}\")"
],
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"physical cores: 8\n"
]
}
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 14,
"source": [
"import platform\n",
"\n",
"\n",
"platform.platform()"
],
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'macOS-11.5.1-arm64-i386-64bit'"
]
},
"metadata": {},
"execution_count": 14
}
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 2,
"source": [
"from sklearn.datasets import make_classification\n",
"from sklearn.ensemble import RandomForestClassifier\n",
"\n",
"\n",
"X, y = make_classification(n_samples=int(1e4), n_features=50, random_state=0)\n",
"clf = RandomForestClassifier(n_estimators=100, random_state=0)"
],
"outputs": [],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 3,
"source": [
"%%timeit\n",
"with parallel_backend(\"threading\", n_jobs=1):\n",
" clf.fit(X, y)"
],
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"4.38 s ± 24.2 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 4,
"source": [
"%%timeit\n",
"with parallel_backend(\"threading\", n_jobs=4):\n",
" clf.fit(X, y)"
],
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"1.17 s ± 20.8 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 11,
"source": [
"4.38 / 1.17"
],
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"3.7435897435897436"
]
},
"metadata": {},
"execution_count": 11
}
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 5,
"source": [
"%%timeit\n",
"with parallel_backend(\"threading\", n_jobs=8):\n",
" clf.fit(X, y)"
],
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"822 ms ± 8.61 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 12,
"source": [
"4.38 / 0.822"
],
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"5.328467153284672"
]
},
"metadata": {},
"execution_count": 12
}
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 13,
"source": [
"%%timeit\n",
"with parallel_backend(\"loky\", n_jobs=1):\n",
" clf.fit(X, y)"
],
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"4.34 s ± 10.2 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 7,
"source": [
"%%timeit\n",
"with parallel_backend(\"loky\", n_jobs=4):\n",
" clf.fit(X, y)"
],
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"1.36 s ± 22.3 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": 8,
"source": [
"%%timeit\n",
"with parallel_backend(\"loky\", n_jobs=8):\n",
" clf.fit(X, y)"
],
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"985 ms ± 32.2 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"metadata": {}
},
{
"cell_type": "code",
"execution_count": null,
"source": [],
"outputs": [],
"metadata": {}
}
],
"metadata": {
"orig_nbformat": 4,
"language_info": {
"name": "python",
"version": "3.9.2",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3.9.2 64-bit ('dev': conda)"
},
"interpreter": {
"hash": "88e9b7219fe8fc278badea083ab1c022b86eb291352bb8a883adb96d8f084d4b"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment