Created
June 27, 2022 10:53
-
-
Save joezuntz/dff5e35825d4869800af9aca3f268a31 to your computer and use it in GitHub Desktop.
Parallel loadz using concurrent.futures
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"id": "9e0e063f", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np\n", | |
"import concurrent.futures" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"id": "bc48edbe", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def make_fake_data():\n", | |
" for i in range(20):\n", | |
" x = np.random.uniform(size=100_000_000)\n", | |
" np.savez(f\"./{i}.npz\", x)\n", | |
"\n", | |
"make_fake_data()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"id": "7f1e0797", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def load_one_file(i):\n", | |
" return np.load(f\"./{i}.npz\")['arr_0']\n", | |
" \n", | |
"def load_serial():\n", | |
" return [load_one_file(i) for i in range(20)]\n", | |
" \n", | |
"def load_parallel(max_workers):\n", | |
" with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor:\n", | |
" X = executor.map(load_one_file, range(20))\n", | |
" return list(X)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"id": "957dc96e", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"CPU times: user 6.98 s, sys: 10.4 s, total: 17.3 s\n", | |
"Wall time: 19 s\n" | |
] | |
} | |
], | |
"source": [ | |
"%time X = load_serial()\n", | |
"del X" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"id": "560710dd", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"CPU times: user 6.71 s, sys: 10.1 s, total: 16.8 s\n", | |
"Wall time: 17.6 s\n" | |
] | |
} | |
], | |
"source": [ | |
"%time X = load_parallel(1)\n", | |
"del X" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"id": "ec5b9be6", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"CPU times: user 8.9 s, sys: 13.7 s, total: 22.6 s\n", | |
"Wall time: 14.7 s\n" | |
] | |
} | |
], | |
"source": [ | |
"%time X = load_parallel(2)\n", | |
"del X" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"id": "836cc182", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"CPU times: user 11.6 s, sys: 19.6 s, total: 31.2 s\n", | |
"Wall time: 13.9 s\n" | |
] | |
} | |
], | |
"source": [ | |
"%time X = load_parallel(4)\n", | |
"del X" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"id": "53497a5c", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"CPU times: user 11.8 s, sys: 21.4 s, total: 33.2 s\n", | |
"Wall time: 15.6 s\n" | |
] | |
} | |
], | |
"source": [ | |
"%time X = load_parallel(6)\n", | |
"del X" | |
] | |
} | |
], | |
"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.9.12" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment