1 | #!/usr/bin/env python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | #************************************************************************ |
---|
4 | # TODO AP |
---|
5 | # - add description of file |
---|
6 | # - check of license of book content |
---|
7 | #************************************************************************ |
---|
8 | """ |
---|
9 | @Author: Anne Philipp (University of Vienna) |
---|
10 | |
---|
11 | @Date: March 2018 |
---|
12 | |
---|
13 | @License: |
---|
14 | (C) Copyright 2018. |
---|
15 | |
---|
16 | This software is licensed under the terms of the Apache Licence Version 2.0 |
---|
17 | which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
---|
18 | |
---|
19 | @Requirements: |
---|
20 | A standard python 2.6 or 2.7 installation |
---|
21 | |
---|
22 | @Description: |
---|
23 | ... |
---|
24 | """ |
---|
25 | # ------------------------------------------------------------------------------ |
---|
26 | # MODULES |
---|
27 | # ------------------------------------------------------------------------------ |
---|
28 | from functools import wraps |
---|
29 | import time |
---|
30 | |
---|
31 | # ------------------------------------------------------------------------------ |
---|
32 | # FUNCTION |
---|
33 | # ------------------------------------------------------------------------------ |
---|
34 | def timefn(fn): |
---|
35 | ''' |
---|
36 | @Description: |
---|
37 | Decorator function. It takes the inner function as an argument. |
---|
38 | ''' |
---|
39 | @wraps(fn) |
---|
40 | def measure_time(*args, **kwargs): |
---|
41 | ''' |
---|
42 | @Descripton: |
---|
43 | Passes the arguments through fn for execution. Around the |
---|
44 | execution of fn the time is captured to execute the fn function |
---|
45 | and prints the result along with the function name. |
---|
46 | |
---|
47 | This is taken from the book "High Performance Python" from |
---|
48 | Micha Gorelick and Ian Ozsvald, O'Reilly publisher, 2014, |
---|
49 | ISBN: 978-1-449-36159-4 |
---|
50 | |
---|
51 | @Input: |
---|
52 | *args: undefined |
---|
53 | A variable number of positional arguments. |
---|
54 | |
---|
55 | **kwargs: undefined |
---|
56 | A variable number of key/value arguments. |
---|
57 | |
---|
58 | @Return: |
---|
59 | <nothing> |
---|
60 | ''' |
---|
61 | |
---|
62 | t1 = time.time() |
---|
63 | result = fn(*args, **kwargs) |
---|
64 | t2 = time.time() |
---|
65 | print("@timefn:" + fn.func_name + " took " + str(t2 - t1) + " seconds") |
---|
66 | |
---|
67 | return result |
---|
68 | |
---|
69 | return measure_time |
---|