Browse Source

Improve perf

pull/63/merge
Florian Mounier 12 years ago
parent
commit
07facb7df1
  1. 41
      perf.py
  2. 14
      pygal/_compat.py
  3. 7
      pygal/graph/base.py
  4. 2
      pygal/graph/line.py
  5. 6
      pygal/svg.py

41
perf.py

@ -42,8 +42,47 @@ def prt(s):
sys.stdout.write(s)
sys.stdout.flush()
charts = CHARTS_NAMES if '--all' in sys.argv else 'Line',
if '--profile' in sys.argv:
import cProfile
c = perf('Line', 500, 500)
cProfile.run("c.render()")
sys.exit(0)
if '--mem' in sys.argv:
_TWO_20 = float(2 ** 20)
import os
import psutil
import linecache
pid = os.getpid()
process = psutil.Process(pid)
import gc
gc.set_debug(gc.DEBUG_UNCOLLECTABLE | gc.DEBUG_INSTANCES | gc.DEBUG_OBJECTS)
def print_mem():
mem = process.get_memory_info()[0] / _TWO_20
f = sys._getframe(1)
line = linecache.getline(f.f_code.co_filename, f.f_lineno - 1).replace('\n', '')
print('%s:%d \t| %.6f \t| %s' % (
f.f_code.co_name, f.f_lineno, mem, line))
c = perf('Line', 100, 500)
print_mem()
a = c.render()
print_mem()
import objgraph
objgraph.show_refs([c], filename='sample-graph.png')
gc.collect()
print_mem()
print(gc.garbage)
print_mem()
del a
print_mem()
del c
print_mem()
sys.exit(0)
charts = CHARTS_NAMES if '--all' in sys.argv else 'Line',
for chart in charts:
prt('%s\n' % chart)

14
pygal/_compat.py

@ -19,13 +19,15 @@
import sys
if sys.version_info[0] == 3:
base = (str, bytes)
coerce = str
else:
base = basestring
coerce = unicode
def to_str(string):
if sys.version_info[0] == 3:
base = (str, bytes)
coerce = str
else:
base = basestring
coerce = unicode
if not isinstance(string, base):
return coerce(string)
return string

7
pygal/graph/base.py

@ -38,6 +38,7 @@ class BaseGraph(object):
def __init__(self, config, series, secondary_series, uuid):
"""Init the graph"""
self.uuid = uuid
self.__dict__.update(config.to_dict())
self.config = config
self.series = series or []
self.secondary_series = secondary_series or []
@ -68,12 +69,6 @@ class BaseGraph(object):
self._draw()
self.svg.pre_render()
def __getattr__(self, attr):
"""Search in config, then in self"""
if hasattr(self.config, attr):
return object.__getattribute__(self.config, attr)
return object.__getattribute__(self, attr)
@property
def all_series(self):
return self.series + self.secondary_series

2
pygal/graph/line.py

@ -78,7 +78,6 @@ class Line(Graph):
if y > self.view.height / 2:
classes.append('top')
classes = ' '.join(classes)
dots = decorate(
self.svg,
self.svg.node(serie_node['overlay'], class_="dots"),
@ -132,5 +131,6 @@ class Line(Graph):
def _plot(self):
for index, serie in enumerate(self.series):
self.line(self._serie(index), serie)
for index, serie in enumerate(self.secondary_series, len(self.series)):
self.line(self._serie(index), serie, True)

6
pygal/svg.py

@ -66,7 +66,7 @@ class Svg(object):
def add_styles(self):
"""Add the css to the svg"""
colors = self.graph.style.get_colors(self.id)
colors = self.graph.config.style.get_colors(self.id)
all_css = []
for css in ['base.css'] + list(self.graph.css):
if '://' in css:
@ -80,9 +80,9 @@ class Svg(object):
with io.open(css, encoding='utf-8') as f:
css_text = template(
f.read(),
style=self.graph.style,
style=self.graph.config.style,
colors=colors,
font_sizes=self.graph.font_sizes(),
font_sizes=self.graph.config.font_sizes(),
id=self.id)
if not self.graph.pretty_print:
css_text = minify_css(css_text)

Loading…
Cancel
Save