Skip to content

Instantly share code, notes, and snippets.

@davidshinn
Last active August 29, 2015 14:15
Show Gist options
  • Save davidshinn/3195cf5cc67cd86b2ad2 to your computer and use it in GitHub Desktop.
Save davidshinn/3195cf5cc67cd86b2ad2 to your computer and use it in GitHub Desktop.
Profile finding max in vector versus max in matrix
{"nbformat_minor": 0, "cells": [{"source": "### Profile finding max in a vector versus finding max in matrix ", "cell_type": "markdown", "metadata": {}}, {"execution_count": 1, "cell_type": "code", "source": "import numpy as np", "outputs": [], "metadata": {"collapsed": true, "trusted": true}}, {"execution_count": 2, "cell_type": "code", "source": "%%time\nm = np.random.randn(500, 600, 500)", "outputs": [{"output_type": "stream", "name": "stdout", "text": "CPU times: user 4.37 s, sys: 309 ms, total: 4.68 s\nWall time: 4.68 s\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"execution_count": 3, "cell_type": "code", "source": "v = m[:, :, 1]", "outputs": [], "metadata": {"collapsed": true, "trusted": true}}, {"source": "Timing finding max in a vector\n\nHowever, realize you have to run this 300,000 times (500\\*600)", "cell_type": "markdown", "metadata": {}}, {"execution_count": 4, "cell_type": "code", "source": "%timeit np.max(v)", "outputs": [{"output_type": "stream", "name": "stdout", "text": "100 loops, best of 3: 3.42 ms per loop\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "Timing finding max in a matrix\n\nRemember, you only have to run this once if done this way", "cell_type": "markdown", "metadata": {}}, {"execution_count": 5, "cell_type": "code", "source": "%timeit np.max(m, axis=2)", "outputs": [{"output_type": "stream", "name": "stdout", "text": "10 loops, best of 3: 98.6 ms per loop\n"}], "metadata": {"collapsed": false, "trusted": true}}], "nbformat": 4, "metadata": {"kernelspec": {"display_name": "IPython (Python 2)", "name": "python2"}, "language_info": {"mimetype": "text/x-python", "nbconvert_exporter": "python", "version": "2.7.9", "name": "python", "file_extension": ".py", "pygments_lexer": "ipython2", "codemirror_mode": {"version": 2, "name": "ipython"}}}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment