In [1]:
import copy
from fireplace.exceptions import GameOver
from fireplace.utils import play_full_game
import logging
import random
from fireplace import cards
import pstats
%load_ext snakeviz
cards.db.initialize()
logging.disable(logging.WARNING) #logging.NOTSET
In [2]:
def run_game():
try:
play_full_game();
except GameOver:
pass
In [3]:
%%capture c
random.seed(1857)
%timeit -n100 run_game()
In [4]:
c.stdout.splitlines()[-1]
Out[4]:
In [5]:
%%capture
%%prun -D fireplace.prof
random.seed(1857)
for _ in range(100):
run_game()
In [6]:
stats_f = 'fireplace.prof'
stats = pstats.Stats(stats_f)
stats.strip_dirs()
stats.dump_stats(stats_f)
In [33]:
import pandas as pd
from io import StringIO
index = pd.MultiIndex.from_tuples(stats.stats.keys())
df = pd.DataFrame(list(stats.stats.values()), index=index)
df.drop(df.columns[[4]], axis=1, inplace=True)
df.columns = ['ncalls', 'nreccalls', 'tottime', 'cumtime']
df.index.rename(['filename', 'lineno', 'function'], inplace=True)
df.sort_values(by='tottime', ascending=False)
Out[33]:
In [10]:
pd.options.display.float_format = '{:.2f}'.format
c = 'tottime'
breakdown = (pd.DataFrame(df.groupby(level='filename').sum()[c] / df[c].sum())
.sort_values(by=c, ascending=False))
breakdown
Out[10]:
In [32]:
from matplotlib import pyplot as plt
plt.style.use('ggplot')
%matplotlib inline
f, ax = plt.subplots(figsize=(10, 10))
breakdown.plot.pie(subplots=True, figsize=(10, 10), ax=ax, autopct='%.1f%%', fontsize=14)
ax.legend().set_visible(False)
plt.ylabel('')
Out[32]:
In [45]:
import plotly.plotly as py
import cufflinks as cf
cf.set_config_file(world_readable=True,offline=False)
breakdown['file'] = breakdown.index
breakdown.iplot(kind='pie', labels='file', values='tottime')
Out[45]: