Created
October 20, 2013 15:53
-
-
Save jtratner/7071390 to your computer and use it in GitHub Desktop.
Python int-check performance comparison
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"name": "Perf comparison for int check between try-except and instancecheck" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "# perf comparison\nimport numbers\ndef try_except(val):\n try:\n return val == int(val)\n except:\n return False\n\ndef check_integral(val):\n return isinstance(val, (int, numbers.Integral))", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 4 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": "false_vals = ['a', 'x', '1', 1.25, 3.21, 0.0000234234234,\n '13234235029352039590235902390590230592', 'ace', None]\ntrue_vals = [1, 2, 3, 4, 5, 6, 1341342034902590L]\nfor x in false_vals:\n print(repr(x))\n assert not try_except(x)\n assert not check_integral(x)\n print(\"try_except\")\n %timeit try_except(x)\n print(\"check_integral\")\n %timeit check_integral(x)\nfor x in true_vals:\n print(repr(x))\n assert try_except(x)\n assert check_integral(x)\n print(\"try_except\")\n %timeit try_except(x)\n print(\"check_integral\")\n %timeit check_integral(x)", | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "'a'\ntry_except\n100000 loops, best of 3: 3.83 \u00b5s per loop" | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "\ncheck_integral\n100000 loops, best of 3: 4.08 \u00b5s per loop" | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "\n'x'\ntry_except\n100000 loops, best of 3: 4.74 \u00b5s per loop" | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "\ncheck_integral\n100000 loops, best of 3: 4.34 \u00b5s per loop" | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "\n'1'\ntry_except\n1000000 loops, best of 3: 1.59 \u00b5s per loop" | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "\ncheck_integral\n100000 loops, best of 3: 4.17 \u00b5s per loop" | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "\n1.25\ntry_except\n1000000 loops, best of 3: 582 ns per loop" | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "\ncheck_integral\n100000 loops, best of 3: 4.16 \u00b5s per loop" | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "\n3.21\ntry_except\n1000000 loops, best of 3: 570 ns per loop" | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "\ncheck_integral\n100000 loops, best of 3: 4.14 \u00b5s per loop" | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "\n2.34234234e-05\ntry_except\n1000000 loops, best of 3: 614 ns per loop" | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "\ncheck_integral\n100000 loops, best of 3: 4.35 \u00b5s per loop" | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "\n'13234235029352039590235902390590230592'\ntry_except\n1000000 loops, best of 3: 1.79 \u00b5s per loop" | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "\ncheck_integral\n100000 loops, best of 3: 4.43 \u00b5s per loop" | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "\n'ace'\ntry_except\n100000 loops, best of 3: 4 \u00b5s per loop" | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "\ncheck_integral\n100000 loops, best of 3: 4.22 \u00b5s per loop" | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "\nNone\ntry_except\n100000 loops, best of 3: 3.17 \u00b5s per loop" | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "\ncheck_integral\n100000 loops, best of 3: 4.27 \u00b5s per loop" | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "\n1\ntry_except\n1000000 loops, best of 3: 505 ns per loop" | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "\ncheck_integral\n1000000 loops, best of 3: 559 ns per loop" | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "\n2\ntry_except\n1000000 loops, best of 3: 505 ns per loop" | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "\ncheck_integral\n1000000 loops, best of 3: 565 ns per loop" | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "\n3\ntry_except\n1000000 loops, best of 3: 496 ns per loop" | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "\ncheck_integral\n1000000 loops, best of 3: 553 ns per loop" | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "\n4\ntry_except\n1000000 loops, best of 3: 496 ns per loop" | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "\ncheck_integral\n1000000 loops, best of 3: 554 ns per loop" | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "\n5\ntry_except\n1000000 loops, best of 3: 480 ns per loop" | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "\ncheck_integral\n1000000 loops, best of 3: 562 ns per loop" | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "\n6\ntry_except\n1000000 loops, best of 3: 492 ns per loop" | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "\ncheck_integral\n1000000 loops, best of 3: 558 ns per loop" | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "\n1341342034902590L\ntry_except\n1000000 loops, best of 3: 759 ns per loop" | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "\ncheck_integral\n100000 loops, best of 3: 2.89 \u00b5s per loop" | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": "\n" | |
} | |
], | |
"prompt_number": 6 | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment