Skip to content

Instantly share code, notes, and snippets.

@aluchies
Last active August 29, 2015 14:02
Show Gist options
  • Save aluchies/7aab08f3fef56a110586 to your computer and use it in GitHub Desktop.
Save aluchies/7aab08f3fef56a110586 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "",
"signature": "sha256:5f7441bf657e6751ef6992211a1fb7940d0c49ce2b58fb2a244a35a9f16292ba"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"import random\n",
"\n",
"def maxForwardDiff1(alist):\n",
" maxDiff = 0\n",
" for i in xrange(len(alist)):\n",
" for j in xrange(i+1, len(alist)):\n",
" if (alist[i] - alist[j]) > maxDiff:\n",
" maxDiff = alist[i] - alist[j]\n",
" \n",
" return maxDiff\n",
"\n",
"def maxForwardDiff2(alist):\n",
" sorted_alist = sorted(alist)\n",
" maxDiff = 0\n",
" for i in xrange(0, len(alist) - 1):\n",
" sorted_alist.remove(alist[i])\n",
" if (alist[i] - sorted_alist[0]) > maxDiff:\n",
" maxDiff = alist[i] - sorted_alist[0]\n",
" \n",
" return maxDiff\n",
"\n",
"N = 1000\n",
"alist = [random.random() for _ in xrange(N)]"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%timeit\n",
"maxForwardDiff1(alist)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"10 loops, best of 3: 50.4 ms per loop\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%%timeit\n",
"maxForwardDiff2(alist)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"100 loops, best of 3: 4.5 ms per loop\n"
]
}
],
"prompt_number": 3
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment