{ "metadata": { "name": "", "signature": "sha256:c4be70a71a435c579c5608c88bfbf06a7c72e008f71a75aab9a4bee1a47792a3" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Timing function calls in Python" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import time\n", "\n", "def time_it(f):\n", " time_it.active = 0\n", "\n", " def tt(*args, **kwargs):\n", " time_it.active += 1\n", " t0 = time.time()\n", " tabs = '\\t'*(time_it.active - 1)\n", " name = f.__name__\n", " print('{tabs}Executing <{name}>'.format(tabs=tabs, name=name))\n", " res = f(*args, **kwargs)\n", " print('{tabs}Function <{name}> execution time: {time:.3f} seconds'.format(\n", " tabs=tabs, name=name, time=time.time() - t0))\n", " time_it.active -= 1\n", " return res\n", " return tt\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 79 }, { "cell_type": "code", "collapsed": false, "input": [ "import time\n", "import scipy.ndimage as nd\n", "import numpy as n\n", "\n", "@time_it\n", "def do_all(data, blurs):\n", " for blur in blurs:\n", " process(data, blur)\n", " \n", "@time_it\n", "def process(data, amount):\n", " blurred = nd.gaussian_filter(data, amount)\n", " out = get_stats(blurred)\n", " return out\n", "\n", "@time_it\n", "def get_stats(c):\n", " return c.mean(), c.std()" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 80 }, { "cell_type": "code", "collapsed": false, "input": [ "data = n.random.randn(5000,5000)\n", "blurs = n.arange(0,40,10)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 10 }, { "cell_type": "code", "collapsed": false, "input": [ "do_all(data, blurs)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Executing \n", "\tExecuting \n", "\t\tExecuting " ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\t\tFunction execution time: 0.229 seconds" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\tFunction execution time: 0.350 seconds\n", "\tExecuting \n", "\t\tExecuting " ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\t\tFunction execution time: 0.244 seconds" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\tFunction execution time: 3.261 seconds\n", "\tExecuting \n", "\t\tExecuting " ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\t\tFunction execution time: 0.224 seconds" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\tFunction execution time: 5.048 seconds\n", "\tExecuting \n", "\t\tExecuting " ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\t\tFunction execution time: 0.278 seconds" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "\tFunction execution time: 6.958 seconds\n", "Function execution time: 15.617 seconds\n" ] } ], "prompt_number": 81 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }