File size: 2,900 Bytes
6250360
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#encoding=utf-8
import logging
import time
def print_calling(fn):
    def wrapper(*args1, ** args2):
        s = "calling function %s"%(fn.__name__)
        logging.info(s)
        start = time.time()
        ret = fn(*args1, **args2)
        end = time.time()
#         s = "%s. time used = %f seconds"%(s, (end - start))
        s = "function [%s] has been called, taking %f seconds"%(fn.__name__, (end - start))
        logging.debug(s)
        return ret
    return wrapper


def print_test(fn):
    def wrapper(*args1, ** args2):
        s = "running test: %s..."%(fn.__name__)
        logging.info(s)
        ret = fn(*args1, **args2)
        s = "running test: %s...succeed"%(fn.__name__)
        logging.debug(s)
        return ret
    return wrapper
    
def print_calling_in_short(fn):
    def wrapper(*args1, ** args2):
        start = time.time()
        ret = fn(*args1, **args2)
        end = time.time()
        s = "function [%s] has been called, taking %f seconds"%(fn.__name__, (end - start))
        logging.debug(s)
        return ret
    return wrapper

import collections
counter = collections.defaultdict(int)
count_times =collections.defaultdict(int)
def print_calling_in_short_for_tf(fn):
    import tensorflow as tf
    import util
    def wrapper(*args1, ** args2):
        start = time.time()
        thread_name = util.thread.get_current_thread_name()
        ret = fn(*args1, **args2)
        end = time.time()
        counter[fn.__name__] = counter[fn.__name__] + (end - start)
        count_times[fn.__name__] += 1
        all_time = sum([counter[name] for name in counter]) * 1.0
        for name in counter:
#             tf.logging.info('\t %s: %f, %f seconds'%(name, counter[name] / all_time, counter[name]))
            tf.logging.info('\t %s: %d callings, %fsper calling'%(name, count_times[name], counter[name] * 1.0 / count_times[name]))
        s = "Thread [%s]:function [%s] has been called, taking %f seconds"%(thread_name, fn.__name__, (end - start))
        tf.logging.info(s)
        return ret
    return wrapper

def timeit(fn):
    import util
    def wrapper(*args1, ** args2):
        start = time.time()
        thread_name = util.thread.get_current_thread_name()
        ret = fn(*args1, **args2)
        end = time.time()
        counter[fn.__name__] = counter[fn.__name__] + (end - start)
        count_times[fn.__name__] += 1
        all_time = sum([counter[name] for name in counter]) * 1.0
        for name in counter:
            logging.info('\t %s: %f, %f seconds'%(name, counter[name] / all_time, counter[name]))
            logging.info('\t %s: %d callings, %f seconds per calling'%(name, count_times[name], counter[name] * 1.0 / count_times[name]))
        s = "Thread [%s]:function [%s] has been called, taking %f seconds"%(thread_name, fn.__name__, (end - start))
#         logging.info(s)
        return ret
    return wrapper