Skip to content

Instantly share code, notes, and snippets.

@mahenzon
Created March 26, 2023 18:17
Show Gist options
  • Save mahenzon/a0c852e1996c74a3ef2e821879501b6d to your computer and use it in GitHub Desktop.
Save mahenzon/a0c852e1996c74a3ef2e821879501b6d to your computer and use it in GitHub Desktop.
Python compare timeit vs time and use perf_counter
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Slept for 1.00257s\n"
]
}
],
"source": [
"from time import time, sleep\n",
"\n",
"time_now = time()\n",
"sleep(1)\n",
"total = time() - time_now\n",
"print(f\"Slept for {total:.5f}s\")"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def sum_to_n(n):\n",
" result = 0\n",
" for i in range(1, n + 1):\n",
" result += i\n",
" return result"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" 3 => 6\n",
" 4 => 10\n",
" 10 => 55\n",
"100 => 5050\n"
]
}
],
"source": [
"print(\" 3 =>\", sum_to_n(3))\n",
"print(\" 4 =>\", sum_to_n(4))\n",
"print(\" 10 =>\", sum_to_n(10))\n",
"print(\"100 =>\", sum_to_n(100))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"from time import time"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"time taken 1: 0.0001590\n"
]
}
],
"source": [
"start_time = time()\n",
"sum_to_n(1000)\n",
"elapsed_time = time() - start_time\n",
"print(f\"time taken 1: {elapsed_time:.7f}\")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"time taken 2: 0.0001373\n"
]
}
],
"source": [
"start_time = time()\n",
"sum_to_n(1000)\n",
"elapsed_time = time() - start_time\n",
"print(f\"time taken 2: {elapsed_time:.7f}\")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"time taken 3: 0.0001307\n"
]
}
],
"source": [
"start_time = time()\n",
"sum_to_n(1000)\n",
"elapsed_time = time() - start_time\n",
"print(f\"time taken 3: {elapsed_time:.7f}\")"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.21287670801393688\n"
]
}
],
"source": [
"from timeit import timeit\n",
"\n",
"\n",
"print(\n",
" timeit(\n",
" \"sum_to_n(1000)\",\n",
" globals={\n",
" \"sum_to_n\": sum_to_n,\n",
" },\n",
" number=10_000,\n",
" )\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2.1287670801393688e-05"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"0.21287670801393688 / 10_000"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.0000212877\n"
]
}
],
"source": [
"print(\"{:.10f}\".format(0.21287670801393688 / 10_000))"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.00010941230000000001"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"0.0001307 - 0.0000212877"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6.139695692817919"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"0.0001307 / 0.0000212877"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"time taken for 10k: 0.23230\n"
]
}
],
"source": [
"start_time = time()\n",
"for _ in range(10_000):\n",
" sum_to_n(1000)\n",
"\n",
"elapsed_time = time() - start_time\n",
"print(f\"time taken for 10k: {elapsed_time:.5f}\")"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"def sum_to_n_opt(n):\n",
" return n * (n + 1) // 2"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"with cycle: 2.5603845\n",
"optimized: 0.0000082\n"
]
}
],
"source": [
"num_for_count = 1_000_000\n",
"number_iter = 100\n",
"\n",
"time_old = timeit(\n",
" \"sum_func(n)\",\n",
" globals={\n",
" \"sum_func\": sum_to_n,\n",
" \"n\": num_for_count,\n",
" },\n",
" number=number_iter,\n",
")\n",
"print(f\"with cycle: {time_old:.7f}\")\n",
"\n",
"time_new = timeit(\n",
" \"sum_func(n)\",\n",
" globals={\n",
" \"sum_func\": sum_to_n_opt,\n",
" \"n\": num_for_count,\n",
" },\n",
" number=number_iter, \n",
")\n",
"print(f\"optimized: {time_new:.7f}\")"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"time taken (opt): 0.0000720\n"
]
}
],
"source": [
"start_time = time()\n",
"sum_to_n_opt(1000)\n",
"elapsed_time = time() - start_time\n",
"print(f\"time taken (opt): {elapsed_time:.7f}\")"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1.8152777777777778"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"0.0001307 / 0.0000720"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"time taken (old): 0.0484049320\n",
"time taken (new): 0.0000302792\n"
]
}
],
"source": [
"number = 1_000_000\n",
"\n",
"start_time = time()\n",
"sum_to_n(number)\n",
"elapsed_time = time() - start_time\n",
"print(f\"time taken (old): {elapsed_time:.10f}\")\n",
" \n",
"\n",
"start_time = time()\n",
"sum_to_n_opt(number)\n",
"elapsed_time = time() - start_time\n",
"print(f\"time taken (new): {elapsed_time:.10f}\")"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [],
"source": [
"from functools import wraps\n",
"from timeit import default_timer\n",
"\n",
"\n",
"def timeit_dec(func):\n",
" @wraps(func)\n",
" def wrapper(*args, **kwargs):\n",
" # Measure execution time using default_timer\n",
" start_time = default_timer()\n",
" result = func(*args, **kwargs)\n",
" execution_time = default_timer() - start_time\n",
"\n",
" # Print elapsed time\n",
" print(f\"Execution time for {func.__name__}: {execution_time:.9f} seconds\")\n",
"\n",
" # Return the result of the decorated function\n",
" return result\n",
" return wrapper"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [],
"source": [
"@timeit_dec\n",
"def sum_to_n(n):\n",
" result = 0\n",
" for i in range(1, n + 1):\n",
" result += i\n",
" return result"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Execution time for sum_to_n: 0.291768000 seconds\n"
]
},
{
"data": {
"text/plain": [
"50000005000000"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sum_to_n(10_000_000)"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [],
"source": [
"@timeit_dec\n",
"def sum_to_n_opt(n):\n",
" return n * (n + 1) // 2"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Execution time for sum_to_n_opt: 0.000002333 seconds\n"
]
},
{
"data": {
"text/plain": [
"50000005000000"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sum_to_n_opt(10_000_000)"
]
},
{
"cell_type": "code",
"execution_count": null,
"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.11.2"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment