Merge lp:~kolmis/cairoplot/speedup into lp:cairoplot

Proposed by Karel Kolman
Status: Merged
Merged at revision: 41
Proposed branch: lp:~kolmis/cairoplot/speedup
Merge into: lp:cairoplot
Diff against target: 161 lines
4 files modified
trunk/cairoplot.py (+15/-6)
trunk/seriestests.py (+21/-7)
trunk/testscripts/compare.sh (+22/-0)
trunk/testscripts/timeit.sh (+3/-0)
To merge this branch: bzr merge lp:~kolmis/cairoplot/speedup
Reviewer Review Type Date Requested Status
Rodrigo Moreira Araújo Pending
Review via email: mp+12980@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Karel Kolman (kolmis) wrote :

Hi, i'm experimenting with using cairoplot to render plots in the wxbanker project and i stumbled upon some performance related problems which lead me to examining cairoplot's code. Summary of changes proposed in this branch:

 - speed up x_labels rendering
 +++ (create the rotation matrix once only, apply it for each x_label - this is a major improvement)

 - added a timer.sh script and a test plot with 1000 x_labels to show the performance difference

 - replace rotate(angle), rotate(-angle) pairs with save(), rotate(angle), restore() triples
 +++ (i have absolutely none experience in graphics programming, but i think this is the way to go, since i'd say these two rotations added don't have to result in an identity matrix due to floating number arithmetics and rounding errors)

 - added a script for visual comparison of outputs, it diffs two outputs from seriestests.py and creates diff images in PPM format
 +++ i performed the following test: i created two cairoplot versions:
 ====== 1. all show_text() references removed, the rotate(angle), rotate(-angle) statements stayed
 ====== 2. all show_text() references removed, all rotate() pairs removed
 ====== comparing these two some plots were different whereas they should be exactly the same

Revision history for this message
Karel Kolman (kolmis) wrote :

and i meant a minor improvement, not major :)

Revision history for this message
Rodrigo Moreira Araújo (alf-rodrigo) wrote :

Hello Karel,

I'm sorry for the long time, but I'd like to let you know I just merged your
code onto the main branch.

Thanks a lot for the contribution.

Cheers,

Rodrigo Araujo

On Wed, Oct 7, 2009 at 7:07 AM, Karel Kolman <email address hidden> wrote:

> and i meant a minor improvement, not major :)
> --
> https://code.launchpad.net/~kolmis/cairoplot/speedup/+merge/12980
> You are requested to review the proposed merge of
> lp:~kolmis/cairoplot/speedup into lp:cairoplot.
>

Revision history for this message
Karel Kolman (kolmis) wrote :

Thanks for merging the branch.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'trunk/cairoplot.py'
2--- trunk/cairoplot.py 2009-07-09 21:57:24 +0000
3+++ trunk/cairoplot.py 2009-10-07 10:00:26 +0000
4@@ -510,9 +510,10 @@
5 if self.titles[VERT]:
6 title_width,title_height = cr.text_extents(self.titles[VERT])[2:4]
7 cr.move_to( self.dimensions[HORZ] - self.borders[HORZ] + title_height/2, self.dimensions[VERT]/2 - title_width/2)
8+ cr.save()
9 cr.rotate( math.pi/2 )
10 cr.show_text( self.titles[VERT] )
11- cr.rotate( -math.pi/2 )
12+ cr.restore()
13
14 def render_grid(self):
15 cr = self.context
16@@ -544,21 +545,29 @@
17 cr = self.context
18 step = float( self.plot_width ) / ( len( self.labels[HORZ] ) - 1 )
19 x = self.borders[HORZ]
20+ y = self.dimensions[VERT] - self.borders[VERT] + 5
21+
22+ # store rotation matrix from the initial state
23+ rotation_matrix = cr.get_matrix()
24+ rotation_matrix.rotate(self.x_label_angle)
25+
26+ cr.set_source_rgba(*self.label_color)
27+
28 for item in self.labels[HORZ]:
29- cr.set_source_rgba(*self.label_color)
30 width = cr.text_extents(item)[2]
31- cr.move_to(x, self.dimensions[VERT] - self.borders[VERT] + 5)
32- cr.rotate(self.x_label_angle)
33+ cr.move_to(x, y)
34+ cr.save()
35+ cr.set_matrix(rotation_matrix)
36 cr.show_text(item)
37- cr.rotate(-self.x_label_angle)
38+ cr.restore()
39 x += step
40
41 def render_vert_labels(self):
42 cr = self.context
43 step = ( self.plot_height ) / ( len( self.labels[VERT] ) - 1 )
44 y = self.plot_top
45+ cr.set_source_rgba(*self.label_color)
46 for item in self.labels[VERT]:
47- cr.set_source_rgba(*self.label_color)
48 width = cr.text_extents(item)[2]
49 cr.move_to(self.borders[HORZ] - width - 5,y)
50 cr.show_text(item)
51
52=== modified file 'trunk/seriestests.py'
53--- trunk/seriestests.py 2009-07-09 21:57:24 +0000
54+++ trunk/seriestests.py 2009-10-07 10:00:26 +0000
55@@ -1,8 +1,16 @@
56-import cairo, math, random
57+import cairo, math, sys
58
59 import cairoplot
60 from series import Series
61
62+# non-random data for needs of visual comparison of changes
63+if '--non-random' in sys.argv:
64+ random = lambda : 1.0
65+ print 'Plotting nonrandom data'
66+else:
67+ import random
68+ random = random.random
69+
70 # Line plotting
71 test_scatter_plot = 1
72 test_dot_line_plot = 1
73@@ -48,8 +56,8 @@
74 f = [math.exp(x) for x in t]
75 g = [10*math.cos(x) for x in t]
76 h = [10*math.sin(x) for x in t]
77- erx = [0.1*random.random() for x in t]
78- ery = [5*random.random() for x in t]
79+ erx = [0.1*random() for x in t]
80+ ery = [5*random() for x in t]
81 data = Series({"exp" : [t,f], "cos" : [t,g], "sin" : [t,h]})
82 series_colors = [ (1,0,0), (0,0,0), (0,0,1) ]
83 cairoplot.scatter_plot ( 'cross_r_exponential_series.png', data = data, errorx = [erx,erx], errory = [ery,ery], width = 800, height = 600, border = 20,
84@@ -77,6 +85,12 @@
85 cairoplot.dot_line_plot( 'dot_line_3_series_legend_series.png', data, 400, 300, x_labels = x_labels,
86 axis = True, grid = True, series_legend = True )
87
88+ #Speed test, many x_labels
89+ data = range(1000)
90+ x_labels = [str(x) for x in data]
91+ cairoplot.dot_line_plot( 'dot_line_4_many_x_labels.png', data, 14000, 300, x_labels = x_labels,
92+ axis = True, grid = True, series_legend = True )
93+
94 if test_function_plot :
95 #Default Plot
96 data = lambda x : x**2
97@@ -142,7 +156,7 @@
98 cairoplot.vertical_bar_plot ( 'vbar_8_hy_labels_series.png', data, 600, 200, border = 20, display_values = True, grid = True, x_labels = x_labels, y_labels = y_labels )
99
100 #Large data set
101- data = Series([[10*random.random()] for x in range(50)])
102+ data = Series([[10*random()] for x in range(50)])
103 x_labels = ["large label name oh my god it's big" for x in data]
104 cairoplot.vertical_bar_plot ( 'vbar_9_large_series.png', data, 1000, 800, border = 20, grid = True, rounded_corners = True, x_labels = x_labels )
105
106@@ -182,7 +196,7 @@
107 cairoplot.horizontal_bar_plot ( 'hbar_8_hy_labels_series.png', data, 600, 200, border = 20, series_labels = series_labels, display_values = True, grid = True, x_labels = x_labels, y_labels = y_labels )
108
109 #Large data set
110- data = Series([[10*random.random()] for x in range(25)])
111+ data = Series([[10*random()] for x in range(25)])
112 x_labels = ["large label name oh my god it's big" for x in data]
113 cairoplot.horizontal_bar_plot ( 'hbar_9_large_series.png', data, 1000, 800, border = 20, grid = True, rounded_corners = True, x_labels = x_labels )
114
115@@ -242,8 +256,8 @@
116 f = [math.exp(x) for x in t]
117 g = [10*math.cos(x) for x in t]
118 h = [10*math.sin(x) for x in t]
119- erx = [0.1*random.random() for x in t]
120- ery = [5*random.random() for x in t]
121+ erx = [0.1*random() for x in t]
122+ ery = [5*random() for x in t]
123 data = Series({"exp" : [t,f], "cos" : [t,g], "sin" : [t,h]})
124 series_colors = [ (1,0,0), (0,0,0) ]
125 cairoplot.scatter_plot ( 'scatter_color_themes_series.png', data = data, errorx = [erx,erx], errory = [ery,ery], width = 800, height = 600, border = 20,
126
127=== added directory 'trunk/testscripts'
128=== added file 'trunk/testscripts/compare.sh'
129--- trunk/testscripts/compare.sh 1970-01-01 00:00:00 +0000
130+++ trunk/testscripts/compare.sh 2009-10-07 10:00:26 +0000
131@@ -0,0 +1,22 @@
132+#!/bin/sh
133+
134+if [ $# -ne 3 ]
135+then
136+ echo "Compare .png files in two directories"
137+ echo "Usage: ./compare.sh path1 path2 diffdir"
138+ echo "Example: ./compare.sh . ../other ./diff"
139+ exit
140+fi
141+
142+for dir in $1 $2
143+do
144+ for i in $dir/*.png
145+ do
146+ convert $i $i.tiff
147+ done
148+done
149+
150+for i in `(cd $1; ls *.tiff)`
151+do
152+ perceptualdiff $1/$i $2/$i -output $3/$i.ppm
153+done
154
155=== added file 'trunk/testscripts/timeit.sh'
156--- trunk/testscripts/timeit.sh 1970-01-01 00:00:00 +0000
157+++ trunk/testscripts/timeit.sh 2009-10-07 10:00:26 +0000
158@@ -0,0 +1,3 @@
159+#!/bin/sh
160+DIRNAME=`dirname $0`
161+(cd $DIRNAME/..; python -m timeit -n 1 -v "import seriestests")

Subscribers

People subscribed via source and target branches