{
 "metadata": {
  "name": "fromstr"
 },
 "nbformat": 3,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": true,
     "input": [
      "import fromstr",
      "s = \"100.0 \" * 100000",
      "",
      "def fromstring(s):",
      "    data = np.fromstring(s, sep=\" \", dtype=float)",
      "    return data",
      "",
      "def split_and_array(s):",
      "    rawdata = s.split()",
      "    data = np.array(rawdata, dtype=float)",
      "    return data",
      "",
      "def comp_and_array(s):",
      "    rawdata = [float(d) for d in s.split()]",
      "    data = np.array(rawdata, dtype=float)",
      "    return data"
     ],
     "language": "python",
     "outputs": [],
     "prompt_number": 2
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# compare pure python functions",
      "%timeit fromstring(s)",
      "%timeit split_and_array(s)",
      "%timeit comp_and_array(s)"
     ],
     "language": "python",
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "10 loops, best of 3: 19.8 ms per loop",
        "100 loops, best of 3: 13.7 ms per loop"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "",
        "10 loops, best of 3: 26.7 ms per loop"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "",
        ""
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# compare cythonized functions",
      "%timeit fromstr.fromstring(s)",
      "%timeit fromstr.split_and_array(s)",
      "%timeit fromstr.comp_and_array(s)"
     ],
     "language": "python",
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "10 loops, best of 3: 19.8 ms per loop",
        "100 loops, best of 3: 13.7 ms per loop"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "",
        "100 loops, best of 3: 16.2 ms per loop"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "",
        ""
       ]
      }
     ],
     "prompt_number": 7
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# compare cython functions which use atof()",
      "# the first still uses str.split() and is ",
      "# based on this thread: ",
      "# http://comments.gmane.org/gmane.comp.python.numeric.general/41504",
      "# the second tokenizes the string rather ",
      "# than spliting it but may have much higher",
      "# memory requirements.",
      "%timeit fromstr.split_atof(s)",
      "%timeit fromstr.token_atof(s)"
     ],
     "language": "python",
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "100 loops, best of 3: 10.5 ms per loop",
        "100 loops, best of 3: 8.31 ms per loop"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "",
        ""
       ]
      }
     ],
     "prompt_number": 9
    },
    {
     "cell_type": "code",
     "collapsed": true,
     "input": [
      ""
     ],
     "language": "python",
     "outputs": []
    }
   ]
  }
 ]
}