Skip to content

Instantly share code, notes, and snippets.

@prl900
Created March 21, 2022 23:34
Show Gist options
  • Save prl900/085f09f397f7e74f25a92fbee7fadc93 to your computer and use it in GitHub Desktop.
Save prl900/085f09f397f7e74f25a92fbee7fadc93 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": "69602765",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import numexpr as ne\n",
"import iarray as ia\n",
"\n",
"p1 = np.random.random((100,100,1000)).astype('float32')\n",
"p2 = np.random.random((100,100,1000)).astype('float32')\n",
"p3 = np.random.random((100,100,1000)).astype('float32')\n",
"\n",
"p1_ia = ia.numpy2iarray(p1)\n",
"p2_ia = ia.numpy2iarray(p2)\n",
"p3_ia = ia.numpy2iarray(p3)\n",
"\n",
"expr1 = \"(p1 + p2 + p3) / 3\"\n",
"expr2 = \"2.71828**p1 / p2 * log(p3)\""
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "c9871f75",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 132 ms, sys: 68 ms, total: 200 ms\n",
"Wall time: 51.4 ms\n"
]
},
{
"data": {
"text/plain": [
"0.49992874"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%time\n",
"\n",
"res = ne.evaluate(expr1, local_dict={'p1': p1, 'p2': p2, 'p3': p3})\n",
"res.mean()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "3cf6663b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 229 ms, sys: 20.2 ms, total: 249 ms\n",
"Wall time: 267 ms\n"
]
},
{
"data": {
"text/plain": [
"0.4999297"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%time\n",
"\n",
"res = ia.expr_from_string(expr1, {'p1': p1_ia, 'p2': p2_ia, 'p3': p3_ia}).eval()\n",
"ia.mean(res)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "07b58387",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 1.49 s, sys: 202 ms, total: 1.69 s\n",
"Wall time: 330 ms\n"
]
},
{
"data": {
"text/plain": [
"-48.931897281492255"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%time\n",
"\n",
"res = ne.evaluate(expr2, local_dict={'p1': p1, 'p2': p2, 'p3': p3})\n",
"res.mean()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "591e5c63",
"metadata": {},
"outputs": [],
"source": [
"%%time\n",
"\n",
"res = ia.expr_from_string(expr2, {'p1': p1_ia, 'p2': p2_ia, 'p3': p3_ia}).eval()\n",
"ia.mean(res)\n",
"\n",
"# Makes my kernel die"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "70a65d24",
"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.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