# Algorithm efficiency

• February 6th, 2014, 05:19 PM
lozbritt
Algorithm efficiency
Hi all,

I am making an android racing game. I have a method that draws the track to the screen, the map is bigger than the screen so i clip the bitmap and draw it to the screen hence to draw the track I have to get the visible track points and then draw them. I have noticed that when I enable drawing the track the game lags quite a bit but when i disable it is smooth as expected the code is below if anyone can see any obvious efficiency problems.

Code :

``` public void drawTrack(Canvas canvas) {   if (this.trackPoints != null && this.trackPoints.size() > 0) {   // get points, convert back to screen coords and add them to array, // then // use that arraylist<point> below getCurrentVisibleTrack();   counter++;   if (this.visibleTrackPoints.size() > 0) {   // draw lines Point previousPoint = visibleTrackPoints.get(0); Point nextPoint; // draw track line for (int i = 1; i < visibleTrackPoints.size(); i++) { this.p.setColor(Color.BLACK); this.p.setStrokeWidth(100); nextPoint = visibleTrackPoints.get(i); canvas.drawLine(previousPoint.x, previousPoint.y, nextPoint.x, nextPoint.y, p);   if (i % 2 == 0) { // draw outer track this.p.setColor(Color.WHITE); this.p.setStrokeWidth(5); canvas.drawLine(previousPoint.x, previousPoint.y, nextPoint.x, nextPoint.y, p); }   previousPoint = nextPoint; }   } } }   public void getCurrentVisibleTrack() {   // clear all elements this.visibleTrackPoints.clear();   // loop track array Point point; for (int i = 0; i < this.trackPoints.size(); i++) { point = this.trackPoints.get(i); // if that piece of track is on the screen currently then add to // array this.visibleTrackPoints.add(new Point((int) (point.x - this.mapx), (int) (point.y - this.mapy))); } }```

• February 6th, 2014, 05:45 PM
copeg
Re: Algorithm efficiency
Perhaps I'm missing something, but I don't see any clipping going on - seems all the points in trackPoints is being placed in visibleTrackPoints in the getCurrentVisibleTrack() method. If things seem to be slowing down, I recommend profiling the code using println's and timing (System.currentTimeMillis()) to see how long things actually take for particular steps in the program - helps pinpointing the bottlenecks
• February 7th, 2014, 01:42 AM
lozbritt
Re: Algorithm efficiency
Quote:

Originally Posted by copeg
Perhaps I'm missing something, but I don't see any clipping going on - seems all the points in trackPoints is being placed in visibleTrackPoints in the getCurrentVisibleTrack() method. If things seem to be slowing down, I recommend profiling the code using println's and timing (System.currentTimeMillis()) to see how long things actually take for particular steps in the program - helps pinpointing the bottlenecks

Thanks for the reply, I do the clipping in an updateCamera() method which is seperate from this. I wrote this along time ago but it looks like I am moving every track point by -map.x (map.x being the fullsize map) so every point will be translated to my screen coordinates. Ther is only about 30 trackpoints or so, so I don't think this will slow it down. Could it be the drawing?
• February 7th, 2014, 08:28 AM
copeg
Re: Algorithm efficiency
Quote:

Originally Posted by lozbritt
Could it be the drawing?

Again, profile the code. There is no use optimizing something that doesn't need optimizing - find the bottleneck first, then work on optimizing. My post above gives advice on how to go about finding the bottleneck
• February 7th, 2014, 11:31 AM
lozbritt
Re: Algorithm efficiency
Quote:

Originally Posted by copeg
Again, profile the code. There is no use optimizing something that doesn't need optimizing - find the bottleneck first, then work on optimizing. My post above gives advice on how to go about finding the bottleneck

yes but as I said i commented those 2 functions and it speeds up drastically so its something in there thats why im asking i know that method is the bottle neck.
• February 7th, 2014, 11:47 AM
Norm
Re: Algorithm efficiency
Have you tried isolating the problem further than at your methods level?
There are two calls to the Canvas drawLine() method. Try commenting them both out, then one then the other.