Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 47 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand Down