diff --git a/docs/performance.ipynb b/docs/performance.ipynb new file mode 100644 index 0000000..ba93878 --- /dev/null +++ b/docs/performance.ipynb @@ -0,0 +1,216 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "4e17fc34-43f7-4137-992e-f9c87958b408", + "metadata": {}, + "source": [ + "# IAD Performance\n", + "\n", + "**Scott Prahl**\n", + "\n", + "**Feb 2024**\n", + "\n", + "An attempt to figure out why iad is as slow as it is." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "deea90b6-6a64-432d-a609-b2e85a9483d6", + "metadata": {}, + "outputs": [], + "source": [ + "import cProfile\n", + "import pstats\n", + "\n", + "import numpy as np\n", + "import matplotlib.pyplot as plt\n", + "import scipy.optimize\n", + "import iadpython as iad\n", + "import iadpython.iadc as iadc" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "974ce2e9-29f7-4fa9-8f6a-86dddc1b5877", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Wed Feb 21 14:56:31 2024 sample_stats\n", + "\n", + " 133 function calls in 0.031 seconds\n", + "\n", + " Ordered by: cumulative time\n", + "\n", + " ncalls tottime percall cumtime percall filename:lineno(function)\n", + " 1 0.000 0.000 0.031 0.031 {built-in method builtins.exec}\n", + " 1 0.000 0.000 0.031 0.031 :1()\n", + " 1 0.000 0.000 0.031 0.031 /var/folders/bk/7msms9wj50nfy_l1gjzfyx480000gn/T/ipykernel_71323/1167801377.py:1(sample_function)\n", + " 1 0.000 0.000 0.031 0.031 /Users/prahl/Documents/Code/git/iadpython/iadpython/iadc.py:325(rt)\n", + " 100 0.031 0.000 0.031 0.000 /Users/prahl/Documents/Code/git/iadpython/iadpython/iadc.py:75(basic_rt)\n", + " 1 0.000 0.000 0.000 0.000 /opt/homebrew/lib/python3.11/site-packages/numpy/core/function_base.py:24(linspace)\n", + " 1 0.000 0.000 0.000 0.000 {built-in method numpy.arange}\n", + " 4 0.000 0.000 0.000 0.000 /opt/homebrew/lib/python3.11/site-packages/numpy/core/numeric.py:1855(isscalar)\n", + " 2 0.000 0.000 0.000 0.000 {built-in method numpy.asanyarray}\n", + " 5 0.000 0.000 0.000 0.000 {built-in method builtins.isinstance}\n", + " 1 0.000 0.000 0.000 0.000 :117(__instancecheck__)\n", + " 1 0.000 0.000 0.000 0.000 {method 'reshape' of 'numpy.ndarray' objects}\n", + " 4 0.000 0.000 0.000 0.000 {built-in method numpy.empty}\n", + " 1 0.000 0.000 0.000 0.000 {built-in method _abc._abc_instancecheck}\n", + " 1 0.000 0.000 0.000 0.000 /opt/homebrew/lib/python3.11/site-packages/numpy/core/fromnumeric.py:3176(ndim)\n", + " 1 0.000 0.000 0.000 0.000 {method 'astype' of 'numpy.ndarray' objects}\n", + " 1 0.000 0.000 0.000 0.000 {built-in method builtins.max}\n", + " 1 0.000 0.000 0.000 0.000 /opt/homebrew/lib/python3.11/site-packages/numpy/core/function_base.py:19(_linspace_dispatcher)\n", + " 1 0.000 0.000 0.000 0.000 {built-in method _operator.index}\n", + " 1 0.000 0.000 0.000 0.000 {built-in method builtins.len}\n", + " 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}\n", + " 1 0.000 0.000 0.000 0.000 /opt/homebrew/lib/python3.11/site-packages/numpy/core/fromnumeric.py:3172(_ndim_dispatcher)\n", + " 1 0.000 0.000 0.000 0.000 /opt/homebrew/lib/python3.11/site-packages/numpy/core/multiarray.py:669(result_type)\n", + "\n", + "\n" + ] + }, + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def sample_function():\n", + " nslab=1.5 # ignore boundary reflection\n", + " nslide=1.0 # no glass slides above and below the sample\n", + " b=1 # this is pretty much infinite\n", + " g=0.9 # isotropic scattering is fine\n", + " a = np.linspace(0,1,100) # albedo varies between 0 and 1\n", + " ur1,ut1,uru,utu = iadc.rt(nslab, nslide, a, b, g)\n", + "\n", + "# Profile the function and store the results\n", + "cProfile.run('sample_function()', 'sample_stats')\n", + "\n", + "# Create a Stats object to analyze the profiled data\n", + "stats = pstats.Stats('sample_stats')\n", + "\n", + "# Sort the results by cumulative time and print the top results\n", + "stats.sort_stats('cumulative').print_stats(25) # Adjust the number to display more or fewer lines" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "ed824e19-c688-4bd1-a23b-085a781142ed", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Wed Feb 21 14:57:47 2024 sample_stats\n", + "\n", + " 7389 function calls (7350 primitive calls) in 12.130 seconds\n", + "\n", + " Ordered by: cumulative time\n", + " List reduced from 179 to 25 due to restriction <25>\n", + "\n", + " ncalls tottime percall cumtime percall filename:lineno(function)\n", + " 1 0.000 0.000 12.130 12.130 {built-in method builtins.exec}\n", + " 1 0.000 0.000 12.130 12.130 :1()\n", + " 1 0.000 0.000 12.130 12.130 /var/folders/bk/7msms9wj50nfy_l1gjzfyx480000gn/T/ipykernel_71323/3444906631.py:1(sample_function)\n", + " 1 0.000 0.000 12.130 12.130 /Users/prahl/Documents/Code/git/iadpython/iadpython/ad.py:435(rt)\n", + " 10 0.000 0.000 12.069 1.207 /Users/prahl/Documents/Code/git/iadpython/iadpython/ad.py:362(rt_matrices)\n", + " 83 10.975 0.132 10.976 0.132 /opt/homebrew/lib/python3.11/site-packages/numpy/linalg/linalg.py:329(solve)\n", + " 10 0.000 0.000 10.755 1.075 /Users/prahl/Documents/Code/git/iadpython/iadpython/combine.py:120(simple_layer_matrices)\n", + " 10 0.000 0.000 10.755 1.075 /Users/prahl/Documents/Code/git/iadpython/iadpython/combine.py:108(simple_single_layer_matrices)\n", + " 10 0.000 0.000 8.158 0.816 /Users/prahl/Documents/Code/git/iadpython/iadpython/combine.py:86(double_until)\n", + " 63 0.008 0.000 8.158 0.129 /Users/prahl/Documents/Code/git/iadpython/iadpython/combine.py:38(add_layers_basic)\n", + " 10 0.000 0.000 2.597 0.260 /Users/prahl/Documents/Code/git/iadpython/iadpython/start.py:151(thinnest_layer)\n", + " 10 0.001 0.000 2.596 0.260 /Users/prahl/Documents/Code/git/iadpython/iadpython/start.py:115(diamond)\n", + " 10 0.001 0.000 1.313 0.131 /Users/prahl/Documents/Code/git/iadpython/iadpython/combine.py:253(add_same_slides)\n", + " 20 0.660 0.033 0.671 0.034 /opt/homebrew/lib/python3.11/site-packages/scipy/linalg/_decomp_lu.py:94(lu_solve)\n", + " 10 0.400 0.040 0.401 0.040 /opt/homebrew/lib/python3.11/site-packages/scipy/linalg/_basic.py:52(solve)\n", + " 10 0.035 0.003 0.059 0.006 /Users/prahl/Documents/Code/git/iadpython/iadpython/ad.py:416(UX1_and_UXU)\n", + " 78 0.020 0.000 0.020 0.000 {method 'reduce' of 'numpy.ufunc' objects}\n", + " 10 0.000 0.000 0.020 0.002 /opt/homebrew/lib/python3.11/site-packages/numpy/core/fromnumeric.py:2836(min)\n", + " 10 0.000 0.000 0.020 0.002 /opt/homebrew/lib/python3.11/site-packages/numpy/core/fromnumeric.py:71(_wrapreduction)\n", + " 50 0.010 0.000 0.010 0.000 /opt/homebrew/lib/python3.11/site-packages/scipy/linalg/_misc.py:181(_datacopied)\n", + " 146 0.001 0.000 0.005 0.000 /opt/homebrew/lib/python3.11/site-packages/numpy/lib/twodim_base.py:306(diagflat)\n", + " 135 0.004 0.000 0.005 0.000 /opt/homebrew/lib/python3.11/site-packages/numpy/core/numeric.py:1855(isscalar)\n", + " 11 0.000 0.000 0.004 0.000 /Users/prahl/Documents/Code/git/iadpython/iadpython/ad.py:173(nu_c)\n", + " 11 0.000 0.000 0.004 0.000 /Users/prahl/Documents/Code/git/iadpython/iadpython/fresnel.py:34(cos_critical)\n", + " 152 0.003 0.000 0.003 0.000 {built-in method numpy.arange}\n", + "\n", + "\n" + ] + }, + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def sample_function():\n", + " nslab=1.5 # ignore boundary reflection\n", + " nslide=1.0 # no glass slides above and below the sample\n", + " b=1 # this is pretty much infinite\n", + " g=0.9 # isotropic scattering is fine\n", + " a = np.linspace(0,1,10) # albedo varies between 0 and 1\n", + " s = iad.Sample(a=a, b=b, g=g, n=nslab, n_above=nslide, n_below=nslide, quad_pts=16)\n", + " ur1,ut1,uru,utu = s.rt()\n", + " \n", + "# Profile the function and store the results\n", + "cProfile.run('sample_function()', 'sample_stats')\n", + "\n", + "# Create a Stats object to analyze the profiled data\n", + "stats = pstats.Stats('sample_stats')\n", + "\n", + "# Sort the results by cumulative time and print the top results\n", + "stats.sort_stats('cumulative').print_stats(25) # Adjust the number to display more or fewer lines" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e1e85d04-8c50-4b9c-8de1-48ffa3c8e07a", + "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.12.3" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}