From ceafafdc421bcdeac9b50e425ddc4c75d0bf0061 Mon Sep 17 00:00:00 2001 From: fenilgmehta <42742240+fenilgmehta@users.noreply.github.com> Date: Tue, 14 Apr 2020 15:19:01 +0530 Subject: [PATCH] Update README.md --- README.md | 69 +++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 47 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 8a6faff..3db2b41 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,7 @@ This is a tiny python package which provides you with a decorator to measure execution time (in ms) of functions. + ## Installation You should be able to install using `pip` in the usual ways: @@ -21,72 +22,96 @@ $ python3 setup.py install Or place the `execution_time` folder that you downloaded somewhere where it can be accessed by your scripts. + ## Basic Usage Import the class `ExecutionTime`, instantiate it and `@obj.timeit` decorater becomes available. ```python +import time +import random from execution_time import ExecutionTime -e = ExecutionTime() +e = ExecutionTime(console=False, module_name=None) + + +def do_something(arg1): + time.sleep(arg1) + + +def hello_world(): + t1 = round(random.random(), 4) # seconds + print('Hello world, time = ' + str(t1*1000) + ' ms') + time.sleep(t1) + @e.timeit def foo(arg1): do_something(arg1) return + @e.timeit def bar(): hello_world() return -foo("dragons") + +foo(2.4) bar() bar() bar() print(e.logtime_data) -## {'foo': {'times_called': 1, 'total_time': 0.0745, 'average_time': 0.0745}, 'bar': {'times_called': 3, 'total_time': 0.2054, 'average_time': 0.0685}} - +# OUTPUT +# {'foo': {'times_called': 1, 'total_time': 2400.2058, 'average_time': 2400.2058}, 'bar': {'times_called': 3, 'total_time': 1776.2484, 'average_time': 592.0828}} ``` `logtime_data` is a dictionary which contains the data in `methodname:time took in ms` format. + ## Additonal Features 1. Auto-Decorate all functions in a script. -This is a shortcut if you want to auto-deocreate all function. To do so, pass the `__name__` attribute to the class instantiation as: + This is a shortcut if you want to auto-deocreate all function. To do so, pass the `__name__` attribute to the class instantiation as: -```python -from execution_time import ExecutionTime + ```python + import time + import random + from execution_time import ExecutionTime -def foo(): - do_something() - return -def bar(): - hi() + def foo(): + time.sleep(1.2) + return -e = ExecutionTime(module_name=__name__) -foo() -bar() + def bar(): + time.sleep(random.random()) + return -print(e.logdata_time) -``` -Note: The class instantiation must be done after functions have been defined and before they are called. This does limit where this feature can be used and the decorator approach is recommeneded in those scenarios. + + e = ExecutionTime(console=False, module_name=__name__) + + foo() + bar() + + print(e.logtime_data) + ``` + Note: The class instantiation must be done after functions have been defined and before they are called. This does limit where this feature can be used and the decorator approach is recommeneded in those scenarios. 2. Provide console logs. -```python -e = ExecutionTime(console=True) -``` + ```python + e = ExecutionTime(console=True) + ``` -Output the log time in console as: `2019-07-06 22:07:55,157 [INFO ] Time take by method : foo is 0.12000000000012001 ms` + Output the log time in console as: `2020-04-14 15:14:34,354 [INFO ] Time take by method : foo is 1201.2767 ms` ## Issues You can report the bugs at the [issue tracker](https://github.com/siddhant-curious/Execution-Time/issues) + ## License Built with ♥ by Siddhant Chhabra([@siddhant-curious](https://github.com/siddhant-curious)) under [MIT License](https://github.com/siddhant-curious/Execution-Time/blob/master/LICENSE)