-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile.py
46 lines (40 loc) · 1.44 KB
/
profile.py
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
"""Profiling and testing (through random actions) of the model
implementation"""
import time
import numpy as np
from pycallgraph import PyCallGraph, Config, GlobbingFilter
from pycallgraph.output import GraphvizOutput
from generator import DefaultGenerator, get_random_action
def main(rand, pcg):
"""Keep randomly exploring embeddings"""
while True:
action_list = []
before = time.time()
embedding = DefaultGenerator().random_embedding(rand)
while True:
action = get_random_action(embedding, rand=rand)
if action is None:
break
action_list.append(action)
pcg.start(reset=False)
embedding.take_action(*action)
pcg.stop()
elapsed_ms = round((time.time() - before) * 1000, 2)
if elapsed_ms > 10000:
pcg.done()
if action is None:
break
actions = len(action_list)
per_action = round(elapsed_ms / actions, 2)
print(f"{elapsed_ms}ms ({actions}, {per_action}ms)")
# import sys
# sys.exit(1)
def profile(rand=np.random):
"""Profile the embedding"""
config = Config()
config.trace_filter = GlobbingFilter(exclude=["pycallgraph.*"])
graphviz = GraphvizOutput(output_file=f"pc.png")
pcg = PyCallGraph(output=graphviz, config=config)
main(rand, pcg)
if __name__ == "__main__":
profile(rand=np.random.RandomState(42))