Created
May 9, 2024 17:26
-
-
Save fomightez/4750870e54fafc6b439f496f1c3c2295 to your computer and use it in GitHub Desktop.
Supporting nb for SO https://stackoverflow.com/a/78454777/8508004
This file contains hidden or 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
| { | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "id": "ff77a3c8-5351-46fa-9730-74d54ce54875", | |
| "metadata": {}, | |
| "source": [ | |
| "## For SO https://stackoverflow.com/a/78454777/8508004\n", | |
| "\n", | |
| "Formatting time / timeout magic results\n", | |
| "\n", | |
| "Note that example post assumed time came out in milliseconds; however, that isn't necessarily the case, for example try `for x in range(10_000_000)` with example code. This implemantation only changes if in micro- or milliseconds per the original post." | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 1, | |
| "id": "ee521f55-2a63-4c9b-884f-f3c7ff36c52f", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "%%capture original_output\n", | |
| "%%timeit -o\n", | |
| "for x in range(100):\n", | |
| " x**2" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "id": "9ffd7184-697f-42cb-a657-9894ef93f9f6", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "0.00004400 s\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "if original_output.stdout.split()[1] == 'us':\n", | |
| " print(f'{(float(original_output.stdout.split()[0])/1000000.0):.8f} s')\n", | |
| "elif original_output.stdout.split()[1] == 'ms':\n", | |
| " print(f'{(float(original_output.stdout.split()[0])/1000.0):.3f} s')\n", | |
| "else:\n", | |
| " print(f'{original_output.stdout.split(\"+-\",1)[0].strip()}')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "id": "9efc63e1-f8bd-4cff-ab15-02783701f55b", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "%%capture original_output\n", | |
| "%%timeit -o\n", | |
| "for x in range(1_000_000):\n", | |
| " x**2" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 4, | |
| "id": "9e4ecdeb-4bb8-4bea-910d-8c7a158ad360", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "0.510 s\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "if original_output.stdout.split()[1] == 'us':\n", | |
| " print(f'{(float(original_output.stdout.split()[0])/1000000.0):.8f} s')\n", | |
| "elif original_output.stdout.split()[1] == 'ms':\n", | |
| " print(f'{(float(original_output.stdout.split()[0])/1000.0):.3f} s')\n", | |
| "else:\n", | |
| " print(f'{original_output.stdout.split(\"+-\",1)[0].strip()}')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 5, | |
| "id": "28f72dc1-2295-4685-89dc-9f08462dc371", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "%%capture original_output\n", | |
| "%%timeit -o\n", | |
| "for x in range(10_000_000):\n", | |
| " x**2" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 6, | |
| "id": "6aaca8c4-b7cb-493b-bed4-f2a74133e645", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "5.07 s\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "if original_output.stdout.split()[1] == 'us':\n", | |
| " print(f'{(float(original_output.stdout.split()[0])/1000000.0):.8f} s')\n", | |
| "elif original_output.stdout.split()[1] == 'ms':\n", | |
| " print(f'{(float(original_output.stdout.split()[0])/1000.0):.3f} s')\n", | |
| "else:\n", | |
| " print(f'{original_output.stdout.split(\"+-\",1)[0].strip()}')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "id": "eab3dc00-468c-48af-bcba-cc0555293f58", | |
| "metadata": {}, | |
| "source": [ | |
| "--------\n", | |
| "\n", | |
| "\n", | |
| "--------\n", | |
| "\n", | |
| "#### Tests/development are below" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "ffed2910-80ff-4e59-829f-7fedd173e5e4", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "%%capture original_output\n", | |
| "%%timeit -o\n", | |
| "for x in range(1_000_000):\n", | |
| " x**2" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "5abd3ccd-a073-42b2-a621-95e211f36396", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "def function():\n", | |
| " for x in range(1_000_000):\n", | |
| " x**2\n", | |
| "\n", | |
| "variable = %timeit -o function()\n", | |
| "print(variable)\n", | |
| "print(type(variable))\n", | |
| "\n", | |
| "print(f'{variable.average:.3} s')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "b9502c96-1bd8-4d84-9f5b-ffa21c29fb64", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "#print(f'{_.average:.3} s')\n", | |
| "print(f'original output: {original_output}')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "979de592-591c-47f9-8577-40b6d9ff82fa", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "%%timeit -o\n", | |
| "for x in range(10_000_000):\n", | |
| " x**2" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "fe77a593-f1ac-41cc-abc6-368773a40af2", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "%%timeit -o\n", | |
| "for x in range(1_000_000):\n", | |
| " x**2" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "03795d41-e4a6-4206-a655-886dde40c4f6", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "type(_)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "34ce0aee-e153-460a-8aac-00b0e847ef02", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "_" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "87c2b9df-695e-465f-a0ab-4513fe1285ca", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "print(f'{_.average} s')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "5ef17cca-1135-4372-a792-c4ea19e305ad", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "print(f'{_.average:.3} s')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "a04e1c3c-17c7-43e9-8233-9dbdbbffbde0", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "print(f'{_.average:.03f} s')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "1f4199f9-eb3f-497c-a9ab-1ac5e52fb28c", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "%%capture original_output\n", | |
| "%%timeit -o\n", | |
| "for x in range(1_000_000):\n", | |
| " x**2" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "cee4ab8c-e1b4-4171-ba40-f1ee7362bbeb", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "original_output.stdout" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "4e869717-5a4f-4f97-a8c8-d3e7118e0606", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "%%capture out_stream\n", | |
| "%%time\n", | |
| "for x in range(1_000_000):\n", | |
| " x**2" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "2a094aeb-cf16-448e-9f05-62b7057c3c22", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "out_stream.stdout" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "09b936eb-4fc7-4077-ba21-f0b2d918938c", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "for x in out_stream.stdout.split(\"\\n\")[-3:]:\n", | |
| " print(x)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "25553a42-aa0c-4ebb-a655-274aaec7c116", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "out_stream.stdout.split(\"\\n\")[-3:]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "8f56c19e-8c0d-4bfa-9ba4-4508288cde1a", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "%%capture original_output\n", | |
| "%%timeit -o\n", | |
| "for x in range(1_000_000):\n", | |
| " x**2" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "a58fa113-7524-4b1c-97b4-6909da7a1964", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "%%capture original_output\n", | |
| "%%timeit -o\n", | |
| "for x in range(10):\n", | |
| " x**2" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "c269b410-4b0a-4c66-91c8-3ed134b5eb17", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "original_output.stdout" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "862fc95b-96d3-4bb2-b0ac-bbb74a06b856", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "if original_output.stdout.split()[1] == 'us':\n", | |
| " print(f'{(float(original_output.stdout.split()[0])/1000000.0):.8f} s')\n", | |
| "elif original_output.stdout.split()[1] == 'ms':\n", | |
| " print(f'{(float(original_output.stdout.split()[0])/1000.0):.3f} s')\n", | |
| "else:\n", | |
| " print(f'{original_output.stdout.split(\"+-\",1)[0].strip()}')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "91ec2f39-72a0-4776-aed9-a120272a98ad", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "original_output.stdout" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "cc5a4f36-a84f-49a5-a891-555a44b762a6", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "f'{int(original_output.stdout.split()[0])}'" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "a441520f-6cd5-4312-971d-96f802be3063", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "original_output.stdout" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "89949e0c-85e3-42a0-b62d-052ed2e510d9", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "original_output.stdout[1] == 'us'" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "09dfdc5b-018b-47df-98c1-410997d1f66c", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "original_output.stdout.split()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "d4284205-cc46-4009-a181-ed2351dfb76b", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "original_output.stdout.split(\"+-\",1)[0].strip()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "cb9d0591-b383-4d77-a5db-f4c7df629300", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "%%capture original_output\n", | |
| "%%timeit -o\n", | |
| "for x in range(10):\n", | |
| " x**2" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "ab275dc2-467f-48b8-bf15-b5f6d5d92468", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "original_output.stdout.split(\"+-\",1)[0].strip()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "id": "f717aae4-8057-4a60-abfc-2bf6e5c780e5", | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python 3 (ipykernel)", | |
| "language": "python", | |
| "name": "python3" | |
| }, | |
| "language_info": { | |
| "codemirror_mode": { | |
| "name": "ipython", | |
| "version": 3 | |
| }, | |
| "file_extension": ".py", | |
| "mimetype": "text/x-python", | |
| "name": "python", | |
| "nbconvert_exporter": "python", | |
| "pygments_lexer": "ipython3", | |
| "version": "3.10.6" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 5 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment