Results 1 to 3 of 3

Thread: Interesting loop in Pascal

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #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.

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
  •