{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Performance of the optimize function from scipy\n", "\n", "This notebook measures the performance of a simple optimize use case." ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "from scipy.optimize import minimize" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [], "source": [ "def rosen(x):\n", " \"\"\"The Rosenbrock function\"\"\"\n", " return 100.0 * (x[1]-x[0]**2.0)**2.0 + (1-x[0])**2.0" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.85 ms ± 232 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n" ] } ], "source": [ "%%timeit \n", "minimize(rosen, [0.0, 0.0], method=\"nelder-mead\",\n", " options={'xtol': 1e-8, 'disp': False})" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2018-07-22 14:33:29.011554\n", "2018-07-22 14:33:32.803683\n" ] } ], "source": [ "import datetime\n", "print(datetime.datetime.now())\n", "for i in range(1000):\n", " minimize(rosen, [0.0, 0.0], method=\"nelder-mead\",\n", " options={'xtol': 1e-8, 'disp': False})\n", "print(datetime.datetime.now())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Let's try numba!" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [], "source": [ "from numba import jit" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [], "source": [ "@jit\n", "def rosen2(x):\n", " \"\"\"The Rosenbrock function\"\"\"\n", " return 100.0 * (x[1]-x[0]**2.0)**2.0 + (1-x[0])**2.0" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.55 ms ± 224 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n" ] } ], "source": [ "%%timeit \n", "minimize(rosen2, [0.0, 0.0], method=\"nelder-mead\",\n", " options={'xtol': 1e-8, 'disp': False})" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [], "source": [ "## mmm... it didn't seem to help." ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [], "source": [ "import numba as nb" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'0.36.2'" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "nb.__version__" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [], "source": [ "import sys" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'3.6.4 |Anaconda custom (64-bit)| (default, Jan 16 2018, 12:04:33) \\n[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)]'" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sys.version" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.6.4" } }, "nbformat": 4, "nbformat_minor": 2 }