Results 1 to 3 of 3

Thread: Interesting loop in Pascal

  1. #1

    Lightbulb Interesting loop in Pascal

    Have you seen anything like this before?

    Code:
      for i:=0 to (Map_width * map_height) do
      begin
            if (i mod Map_Height)=0 then y:=y+1;
     
         x:=i mod Map_width;
    
         // do_something_with_xy
    
        i:=i+1;
      end;
    Is it faster than looping through X,Y?
    One less loop to go?

    Should i also use it to speed things up?
    I haven't measured the which is faster.

  2. #2
    You have several errors in it. I don't know about speed, propably not faster. It can have some modifications which still won't help much. Mod itself on every index makes it heavier than simple for-loop:
    Code:
      y:=-1; // important to initialize y, and know that y gets +1 on first round
      for i:=0 to Map_width * map_height -1 do // added -1, because it starts from 0
      begin
        x:=i mod Map_width;
        if x=0 then y:=y+1; // mod Width, not height, and then we can re-use x
        
        // do_something_with_xy
    
        //i:=i+1; // Don't modify for-loop variable
      end;
    This version however might be faster than the for-loop, or at least equal:
    Code:
      x:=0; y:=0;
      for i:=0 to Map_width * map_height -1 do begin
            
        // do_something_with_xy
    
        x:=x+1;
        if x=Map_width then begin
          x:=0;
          y:=y+1;
        end;
      end;
    Last edited by User137; 18-09-2013 at 01:20 AM.

  3. #3
    Quote Originally Posted by hwnd View Post
    Is it faster than looping through X,Y?
    I seriously doubt it is. Why? Becouse you are introducing two aditional mathematical functions which needs to be executed for every loop iteration.
    I would gues someone used such approach as they stored whole map in 1D array and not 2D array. So "i" always represent index of the map tile in map array.

    And there is another thing in the code that bothers me. What is that thing? Code inside for loop is making changes to for loop control variable. This should be avoided at all times as you can quickly cause infinite loop or compleetly screw up the whole loop.


    Quote Originally Posted by hwnd View Post
    One less loop to go?
    It is not the problem of how many loops you have but what code is in those loops.
    So my advice for you is go and try optimize the code which is inside the loops rather than trying to simply reduce hte number of loops used.
    The loop mechanizm itself is prety fast so reducing the number of loop implementation won't give you much performance gain.
    The only exceptions could be "for in" loops becouse they need to introduce aditional abstract layer to work and that hinders their overal performance. I have talked about this in the thread below:
    http://www.pascalgamedevelopment.com...ighlight=loops

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •