Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Algorithm efficiency

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.


    	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)));
    		}
    	}

    Thanks in advance!


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Algorithm efficiency

    Quote Originally Posted by copeg View Post
    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?

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Algorithm efficiency

    Quote Originally Posted by lozbritt View Post
    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

  5. #5
    Junior Member
    Join Date
    Feb 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Algorithm efficiency

    Quote Originally Posted by copeg View Post
    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.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Efficiency of this snippet?
    By xJavaLover in forum Java Theory & Questions
    Replies: 0
    Last Post: November 30th, 2013, 03:12 PM
  2. Java 2D efficiency
    By dudel in forum Java Theory & Questions
    Replies: 0
    Last Post: April 30th, 2013, 03:50 AM
  3. How to improve this algorithm's efficiency?
    By MariaNiku in forum Algorithms & Recursion
    Replies: 3
    Last Post: January 28th, 2013, 03:58 PM
  4. Efficiency problem
    By mccolem in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 4th, 2011, 03:44 PM
  5. variables and efficiency
    By catkinq in forum Java Theory & Questions
    Replies: 3
    Last Post: February 7th, 2010, 06:09 AM

Tags for this Thread