Letractively / flot

Automatically exported from code.google.com/p/flot
MIT License
0 stars 0 forks source link

Optimising loops over all points in a dataseries #641

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I'm using flot to generate scatter plots with many thousands of datapoints in a 
single series.  In optimising this, I've noticed that there a few instances of 
iterating over all the points as:

for (j = 0; j < points.length; j += ps) {...}

This evaluates the points.length many times, which is inefficient.  I've 
patched my code along the lines of:

var pointlen=points.length;
for (j=0; j<pointlen; j+=ps) {...}

In my instance, it can be made even quicker by running a decreasing loop, but 
I'm not sure that's universal
for (j=points.length()-ps; j>=0; j-=ps) {...} 

Original issue reported on code.google.com by gavinpau...@gmail.com on 25 Nov 2011 at 4:16

GoogleCodeExporter commented 9 years ago
did it speed up anything?
length is a property of Array object, not a method, so it doesn't calculate 
anything when you use points.length 
(http://www.w3schools.com/jsref/jsref_length_array.asp)

Original comment by milke...@gmail.com on 25 Nov 2011 at 5:00

GoogleCodeExporter commented 9 years ago
You're right of course - it only made a 10ms difference on average (on a 
dataset where the loop takes 4000ms to run), and the variability between runs 
was about half this, so probably not significant.
What has made a massive difference, though, is pre-computing the circle symbol 
by plotting it into a small in-memory canvas (outside the loop), and then 
putting a drawImage call into the loop - but I imagine this is a bit too 
specific to be included in the base code.
Thanks for the wonderful library, by the way.

Original comment by gavinpau...@gmail.com on 26 Nov 2011 at 2:27

GoogleCodeExporter commented 9 years ago
There are actually a couple of loop constructs even more efficient than the 
ones you outlined, but the performance difference in all cases is absolutely 
tiny.  And since the standard zero-to-len loop is most familiar to people, it 
wins in terms of clarity, maintainability, and ease of debugging.

So this is a good thought, but I'm marking it as 'wontfix'.  We'll deal with 
loop performance on a case-by-case basis, in response to profiling, rather than 
across-the-board.

Original comment by dnsch...@gmail.com on 23 May 2012 at 2:27

GoogleCodeExporter commented 9 years ago

Original comment by dnsch...@gmail.com on 4 Jun 2012 at 2:41