Skip to content

Instantly share code, notes, and snippets.

@iwatobipen
Created December 13, 2025 07:17
Show Gist options
  • Select an option

  • Save iwatobipen/cbede119994d91cf0698545edd9c7274 to your computer and use it in GitHub Desktop.

Select an option

Save iwatobipen/cbede119994d91cf0698545edd9c7274 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,
"id": "2705ad38-ac17-4f37-a7f8-38decdf7dc85",
"metadata": {
"execution": {
"iopub.execute_input": "2025-12-13T05:43:57.107448Z",
"iopub.status.busy": "2025-12-13T05:43:57.107237Z",
"iopub.status.idle": "2025-12-13T05:43:57.535932Z",
"shell.execute_reply": "2025-12-13T05:43:57.535395Z",
"shell.execute_reply.started": "2025-12-13T05:43:57.107423Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.8.0\n",
"False\n"
]
}
],
"source": [
"import sklearn\n",
"import sys\n",
"print(sklearn.__version__)\n",
"print(sys._is_gil_enabled())"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "6085de42-8235-4007-b6c5-9b88cc44139c",
"metadata": {
"execution": {
"iopub.execute_input": "2025-12-13T05:43:57.536478Z",
"iopub.status.busy": "2025-12-13T05:43:57.536269Z",
"iopub.status.idle": "2025-12-13T05:43:57.597603Z",
"shell.execute_reply": "2025-12-13T05:43:57.597361Z",
"shell.execute_reply.started": "2025-12-13T05:43:57.536462Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(150, 4)\n",
"(569, 30)\n"
]
}
],
"source": [
"from sklearn import svm, datasets\n",
"from sklearn.model_selection import GridSearchCV\n",
"import joblib\n",
"iris = datasets.load_iris()\n",
"data = datasets.load_breast_cancer()\n",
"\n",
"print(iris.data.shape)\n",
"print(data.data.shape)\n",
"parameters = {'kernel':('linear', 'rbf'),\n",
" 'C':[1, 10]}\n",
"svc1 = svm.SVC()\n",
"clf1 = GridSearchCV(svc1, \n",
" param_grid=parameters,\n",
" n_jobs=10)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "99bd0af0-bf9e-42d9-9bdb-f6bf5dd66b20",
"metadata": {
"execution": {
"iopub.execute_input": "2025-12-13T05:43:57.598015Z",
"iopub.status.busy": "2025-12-13T05:43:57.597892Z",
"iopub.status.idle": "2025-12-13T05:46:09.585675Z",
"shell.execute_reply": "2025-12-13T05:46:09.585067Z",
"shell.execute_reply.started": "2025-12-13T05:43:57.597998Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"6.28 s ± 59.9 ms per loop (mean ± std. dev. of 7 runs, 3 loops each)\n"
]
}
],
"source": [
"%%timeit -n 3\n",
"with joblib.parallel_config(backend='threading'):\n",
" clf1.fit(data.data, data.target)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "7164ac60-e615-4421-aa0e-964c7afc7361",
"metadata": {
"execution": {
"iopub.execute_input": "2025-12-13T05:46:09.586149Z",
"iopub.status.busy": "2025-12-13T05:46:09.586005Z",
"iopub.status.idle": "2025-12-13T05:46:09.587861Z",
"shell.execute_reply": "2025-12-13T05:46:09.587582Z",
"shell.execute_reply.started": "2025-12-13T05:46:09.586135Z"
}
},
"outputs": [],
"source": [
"svc2 = svm.SVC()\n",
"clf2 = GridSearchCV(svc2, \n",
" param_grid=parameters,\n",
" n_jobs=10\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "4b15777d-ae4e-4ca0-bd40-ea4a2fdbb8fb",
"metadata": {
"execution": {
"iopub.execute_input": "2025-12-13T05:46:09.588291Z",
"iopub.status.busy": "2025-12-13T05:46:09.588161Z",
"iopub.status.idle": "2025-12-13T05:48:27.235044Z",
"shell.execute_reply": "2025-12-13T05:48:27.234365Z",
"shell.execute_reply.started": "2025-12-13T05:46:09.588278Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"6.55 s ± 189 ms per loop (mean ± std. dev. of 7 runs, 3 loops each)\n"
]
}
],
"source": [
"%%timeit -n 3\n",
"with joblib.parallel_config(backend='loky'):\n",
" clf2.fit(data.data, data.target)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "1ac8480d-1a03-46e4-b984-c616a3636921",
"metadata": {
"execution": {
"iopub.execute_input": "2025-12-13T05:56:44.727396Z",
"iopub.status.busy": "2025-12-13T05:56:44.726968Z",
"iopub.status.idle": "2025-12-13T05:56:44.730656Z",
"shell.execute_reply": "2025-12-13T05:56:44.729818Z",
"shell.execute_reply.started": "2025-12-13T05:56:44.727353Z"
}
},
"outputs": [],
"source": [
"from math import sqrt\n",
"import numpy as np\n",
"from joblib import Parallel, delayed, parallel_backend\n"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "688f6b21-213b-4c8c-9745-5ddc27192eb9",
"metadata": {
"execution": {
"iopub.execute_input": "2025-12-13T06:33:25.279412Z",
"iopub.status.busy": "2025-12-13T06:33:25.279230Z",
"iopub.status.idle": "2025-12-13T06:33:28.507578Z",
"shell.execute_reply": "2025-12-13T06:33:28.507164Z",
"shell.execute_reply.started": "2025-12-13T06:33:25.279397Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"46.1 ms ± 3.35 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
]
}
],
"source": [
"%%timeit -n 10\n",
"with parallel_backend('loky', n_jobs=5):\n",
" a = Parallel()(delayed(np.sqrt)(i ** 2) for i in range(10000))\n"
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "4527816e-8c60-4a4b-8e8c-710d215b142d",
"metadata": {
"execution": {
"iopub.execute_input": "2025-12-13T06:35:04.019013Z",
"iopub.status.busy": "2025-12-13T06:35:04.018680Z",
"iopub.status.idle": "2025-12-13T06:35:14.983238Z",
"shell.execute_reply": "2025-12-13T06:35:14.982884Z",
"shell.execute_reply.started": "2025-12-13T06:35:04.018982Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"157 ms ± 2.42 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
]
}
],
"source": [
"%%timeit -n 10\n",
"with parallel_backend('threading', n_jobs=5):\n",
" a = Parallel()(delayed(np.sqrt)(i ** 2) for i in range(10000))\n"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "46becdc3-b946-495e-8d82-066ee3eced4c",
"metadata": {
"execution": {
"iopub.execute_input": "2025-12-13T06:33:52.864865Z",
"iopub.status.busy": "2025-12-13T06:33:52.864471Z",
"iopub.status.idle": "2025-12-13T06:34:12.357516Z",
"shell.execute_reply": "2025-12-13T06:34:12.357178Z",
"shell.execute_reply.started": "2025-12-13T06:33:52.864827Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"278 ms ± 3.83 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
]
}
],
"source": [
"%%timeit -n 10\n",
"with parallel_backend('multiprocessing', n_jobs=5):\n",
" a = Parallel()(delayed(np.sqrt)(i ** 2) for i in range(10000))\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d6822d6f-d679-4057-80c5-8fb0e75ec538",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.14.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment