Skip to content

Instantly share code, notes, and snippets.

@simecek
Created June 9, 2019 06:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save simecek/8a3f579037dc31d9e91c5043f91e4509 to your computer and use it in GitHub Desktop.
Save simecek/8a3f579037dc31d9e91c5043f91e4509 to your computer and use it in GitHub Desktop.
Time_measuring_decorator.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Time_measuring_decorator.ipynb",
"version": "0.3.2",
"provenance": [],
"collapsed_sections": [],
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/simecek/8a3f579037dc31d9e91c5043f91e4509/time_measuring_decorator.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"metadata": {
"id": "wOcrePav7EES",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
},
"outputId": "82b4cb18-ef74-449e-a8dc-2bbf790f9e53"
},
"source": [
"import datetime\n",
"from numpy.random import rand\n",
"\n",
"class time_decorator:\n",
"\n",
" def __init__(self, f):\n",
" self.f = f\n",
" print(f\"Time decorator for {self.f.__name__} created\")\n",
"\n",
" def __call__(self, *args, **kwargs):\n",
" start = datetime.datetime.now()\n",
" result = self.f(*args, **kwargs)\n",
" duration = datetime.datetime.now() - start\n",
" print(f\"Duration of {self.f.__name__} function call was {duration}.\")\n",
" return result\n",
"\n",
"@time_decorator\n",
"def sum_of_random_numbers(n):\n",
" random_numbers = rand(n)\n",
" return sum(random_numbers)"
],
"execution_count": 1,
"outputs": [
{
"output_type": "stream",
"text": [
"Time decorator for sum_of_random_numbers created\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "2rQWarNO8-AD",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 52
},
"outputId": "ce11f8fb-32f2-4216-d977-9b1b52ad2fb7"
},
"source": [
"sum_of_random_numbers(10_000_000)"
],
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"text": [
"Duration of sum_of_random_numbers function call was 0:00:01.092953.\n"
],
"name": "stdout"
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"5001076.5500206305"
]
},
"metadata": {
"tags": []
},
"execution_count": 2
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment