{
 "metadata": {
  "name": "",
  "signature": "sha256:e867b33c6a4ce64a5d908ffdd79f7bbb2137e02c6992c0509732957e3e97fc59"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "# `log_calls` 0.2.4.post5, profiling the decorator's wrapper"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "The `PROFILE` stuff is commented out in `log_calls.py`. Search `#~ PROFILE` in that file. The relevant numbers:\n",
      "\n",
      "\\# 0.2.4.post5+ profiling (of record_history):\n",
      "        \n",
      "    setup_stackframe_hack         7.5 %\n",
      "    up_to__not_enabled_call       3.3 %\n",
      "    setup_context_init            1.2 %\n",
      "    setup_context_inspect_bind   23.4 %\n",
      "    setup_context_post_bind       8.8 %\n",
      "    setup_context_kwargs_dicts   32.2 %\n",
      "    pre_call_handlers             1.3 %\n",
      "    post_call_handlers           22.3 %"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import numpy as np\n",
      "\n",
      "from log_calls import record_history"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 30
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "@record_history()\n",
      "def f(freq, t):\n",
      "    return np.sin(freq * 2 * np.pi * t)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 31
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "ran_t = np.arange(0.0, 1.0, 1/44100, dtype=np.float32)\n",
      "ran_t"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "metadata": {},
       "output_type": "pyout",
       "prompt_number": 32,
       "text": [
        "array([  0.00000000e+00,   2.26757365e-05,   4.53514731e-05, ...,\n",
        "         9.99931931e-01,   9.99954641e-01,   9.99977291e-01], dtype=float32)"
       ]
      }
     ],
     "prompt_number": 32
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "**Now, naively, call f 44,100 times in a `for` loop:**"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "for t in ran_t: \n",
      "    f(17, t)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 33
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# N = len(f.profile__['setup_stackframe_hack'])   # any of the keys will do\n",
      "sums = {}\n",
      "for key in f.profile__:\n",
      "    sums[key] = sum(f.profile__[key])\n",
      "total = sum([sums[k] for k in sums])\n",
      "for key in f.profile__:\n",
      "    print(\"key: %s, time: %2.6f (%3.1f %%)\" % (key, sums[key], 100*sums[key]/total ))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "key: setup_stackframe_hack, time: 0.558073 (7.5 %)\n",
        "key: up_to__not_enabled_call, time: 0.247157 (3.3 %)\n",
        "key: setup_context_init, time: 0.091691 (1.2 %)\n",
        "key: setup_context_inspect_bind, time: 1.755684 (23.4 %)\n",
        "key: setup_context_post_bind, time: 0.658069 (8.8 %)\n",
        "key: setup_context_kwargs_dicts, time: 2.409068 (32.2 %)\n",
        "key: pre_call_handlers, time: 0.097221 (1.3 %)\n",
        "key: post_call_handlers, time: 1.670122 (22.3 %)\n"
       ]
      }
     ],
     "prompt_number": 29
    }
   ],
   "metadata": {}
  }
 ]
}