1 | #!/usr/bin/env python3 |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | #************************************************************************ |
---|
4 | # ToDo AP |
---|
5 | # - check license of book content |
---|
6 | #************************************************************************ |
---|
7 | #******************************************************************************* |
---|
8 | # |
---|
9 | # @Author: Anne Philipp (University of Vienna) |
---|
10 | # |
---|
11 | # @Date: March 2018 |
---|
12 | # |
---|
13 | # @License: |
---|
14 | # (C) Copyright 2020. |
---|
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 | # @Program functionality: |
---|
20 | # This module is not part of flex_extract. It is just used for testing and |
---|
21 | # performance analysis of some functions. |
---|
22 | # |
---|
23 | # @Program Content: |
---|
24 | # - timefn |
---|
25 | # |
---|
26 | #******************************************************************************* |
---|
27 | |
---|
28 | # ------------------------------------------------------------------------------ |
---|
29 | # MODULES |
---|
30 | # ------------------------------------------------------------------------------ |
---|
31 | from __future__ import print_function |
---|
32 | |
---|
33 | from functools import wraps |
---|
34 | import time |
---|
35 | |
---|
36 | # ------------------------------------------------------------------------------ |
---|
37 | # FUNCTION |
---|
38 | # ------------------------------------------------------------------------------ |
---|
39 | def timefn(func): |
---|
40 | ''' |
---|
41 | @Description: |
---|
42 | Decorator function. It takes the inner function as an argument. |
---|
43 | ''' |
---|
44 | @wraps(func) |
---|
45 | def measure_time(*args, **kwargs): |
---|
46 | ''' |
---|
47 | @Descripton: |
---|
48 | Passes the arguments through fn for execution. Around the |
---|
49 | execution of fn the time is captured to execute the fn function |
---|
50 | and prints the result along with the function name. |
---|
51 | |
---|
52 | This is taken from the book "High Performance Python" from |
---|
53 | Micha Gorelick and Ian Ozsvald, O'Reilly publisher, 2014, |
---|
54 | ISBN: 978-1-449-36159-4 |
---|
55 | |
---|
56 | @Input: |
---|
57 | *args: undefined |
---|
58 | A variable number of positional arguments. |
---|
59 | |
---|
60 | **kwargs: undefined |
---|
61 | A variable number of key/value arguments. |
---|
62 | |
---|
63 | @Return: |
---|
64 | <nothing> |
---|
65 | ''' |
---|
66 | |
---|
67 | time1 = time.time() |
---|
68 | result = func(*args, **kwargs) |
---|
69 | time2 = time.time() |
---|
70 | print("@timefn:" + func.__name__ + " took " + |
---|
71 | str(time2 - time1) + " seconds") |
---|
72 | |
---|
73 | return result |
---|
74 | |
---|
75 | return measure_time |
---|