source: flex_extract.git/source/python/mods/profiling.py @ 12face2

ctbtodev
Last change on this file since 12face2 was 12face2, checked in by Anne Philipp <anne.philipp@…>, 5 years ago

python2 downgrade

  • Property mode set to 100644
File size: 2.3 KB
Line 
1#!/usr/bin/env python
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 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#*******************************************************************************
27
28# ------------------------------------------------------------------------------
29# MODULES
30# ------------------------------------------------------------------------------
31from __future__ import print_function
32
33from functools import wraps
34import time
35
36# ------------------------------------------------------------------------------
37# FUNCTION
38# ------------------------------------------------------------------------------
39def 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()
70        print("@timefn:" + fn.__name__ + " took " + str(t2 - t1) + " seconds")
71
72        return result
73
74    return measure_time
Note: See TracBrowser for help on using the repository browser.
hosted by ZAMG