[8463d78] | 1 | #!/usr/bin/env python3 |
---|
[efdb01a] | 2 | # -*- coding: utf-8 -*- |
---|
| 3 | #************************************************************************ |
---|
[ff99eae] | 4 | # ToDo AP |
---|
[54a8a01] | 5 | # - check license of book content |
---|
[efdb01a] | 6 | #************************************************************************ |
---|
[991df6a] | 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 | # @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 | #******************************************************************************* |
---|
[efdb01a] | 27 | |
---|
| 28 | # ------------------------------------------------------------------------------ |
---|
| 29 | # MODULES |
---|
| 30 | # ------------------------------------------------------------------------------ |
---|
[12face2] | 31 | from __future__ import print_function |
---|
| 32 | |
---|
[efdb01a] | 33 | from functools import wraps |
---|
| 34 | import time |
---|
| 35 | |
---|
| 36 | # ------------------------------------------------------------------------------ |
---|
| 37 | # FUNCTION |
---|
| 38 | # ------------------------------------------------------------------------------ |
---|
| 39 | def timefn(fn): |
---|
| 40 | ''' |
---|
| 41 | @Description: |
---|
| 42 | Decorator function. It takes the inner function as an argument. |
---|
| 43 | ''' |
---|
| 44 | @wraps(fn) |
---|
| 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 | t1 = time.time() |
---|
| 68 | result = fn(*args, **kwargs) |
---|
| 69 | t2 = time.time() |
---|
[79729d5] | 70 | print("@timefn:" + fn.__name__ + " took " + str(t2 - t1) + " seconds") |
---|
[efdb01a] | 71 | |
---|
| 72 | return result |
---|
| 73 | |
---|
| 74 | return measure_time |
---|