2024-heraklion-data/exercises/.ipynb_checkpoints/slow_when_slicing_wrongly-checkpoint.ipynb

153 lines
3 KiB
Plaintext
Raw Normal View History

2024-08-27 14:27:53 +02:00
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "af52b0cd",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "198afa0e",
"metadata": {},
"outputs": [],
"source": [
"def get_slice_and_perform_something(x, axis):\n",
" idx = 75\n",
" if axis == 0:\n",
" slice_ = x[idx, :]\n",
" else:\n",
" slice_ = x[:, idx]\n",
" # Here I divide by two but any other operation will do, \n",
" # we just want to simulate the fact that we actually need to read the memory\n",
" return slice_ // 2"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "15900a3c",
"metadata": {},
"outputs": [],
"source": [
"page_size = 4096\n",
"n = 100\n",
"x = np.empty((page_size * n, page_size * n), dtype='int8')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "c182d3d3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(409600, 409600)"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x.shape"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "86a3e63f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"28.2 µs ± 1.22 µs per loop (mean ± std. dev. of 7 runs, 10,000 loops each)\n"
]
}
],
"source": [
"%timeit get_slice_and_perform_something(x, axis=0)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "8b2a96c2",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The slowest run took 5.29 times longer than the fastest. This could mean that an intermediate result is being cached.\n",
"886 ms ± 337 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%timeit get_slice_and_perform_something(x, axis=1)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "a8f67b13",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"32218.18181818182"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"886000 / 27.5"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "17a65d38",
"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.11.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}