{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Complexity of the algo matters" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": true, "scrolled": true }, "outputs": [], "source": [ "import random\n", "np.random.seed(1234)\n", "pd.set_option('max_rows',12)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def create_frame(N):\n", " return DataFrame(np.random.randn(N,2),columns=list('AB'))" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [], "source": [ "# need top/bottom 5 elements\n", "def by_sorting(df):\n", " s = df.A.order()\n", " return DataFrame({'min' : s.head(5).values, \n", " 'max' : s.tail(5).values })\n", "\n", "def by_n(df):\n", " return DataFrame({'min' : df.A.nsmallest(5).values, \n", " 'max' : df.A.nlargest(5).order().values })" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "N = 1e6\n", "df = create_frame(N)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false, "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result_by_sorting = by_sorting(df)\n", "result_by_n = by_n(df)\n", "result_by_sorting.equals(result_by_n)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
maxmin
04.339092-4.727822
14.359975-4.622794
24.447241-4.556683
34.465920-4.504740
45.190941-4.387650
\n", "
" ], "text/plain": [ " max min\n", "0 4.339092 -4.727822\n", "1 4.359975 -4.622794\n", "2 4.447241 -4.556683\n", "3 4.465920 -4.504740\n", "4 5.190941 -4.387650" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "by_n(df)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10 loops, best of 3: 176 ms per loop\n" ] } ], "source": [ "%timeit by_sorting(df)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10 loops, best of 3: 47.6 ms per loop\n" ] } ], "source": [ "%timeit by_n(df)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "# algos" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": true }, "outputs": [], "source": [ "s = Series(np.random.randint(0,10000,size=10000000))\n", "def f_pandas(s):\n", " return pd.unique(s)\n", "def f_numpy(df):\n", " return np.unique(s.values)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(np.sort(f_pandas(s)) == np.sort(f_numpy(s))).all()" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10 loops, best of 3: 66.6 ms per loop\n" ] } ], "source": [ "%timeit f_pandas(s)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 loops, best of 3: 572 ms per loop\n" ] } ], "source": [ "%timeit f_numpy(s)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.10" } }, "nbformat": 4, "nbformat_minor": 0 }