Page 2 of 2 FirstFirst 12
Results 11 to 18 of 18

Thread: iterator for array structure

  1. #11
    actually I think that migrating iteration to one place in my code would allow me to switch to a flat array world representation at some point.
    but maybe later as half a year passed since project start and still no gameplay..

  2. #12
    @SilverWarrior: IMO, unless you need some record (as in Pascal record type) for each world "tile", enumeration is over engineering it. If nested loops are faster and still do the job, I'd say go for it. Just comment them well so you'll be sure what they do at a later date.
    Quote Originally Posted by User137 View Post
    You create TStopWatch 22 times, but never free it. It is memory leak. Also i guess more interesting numbers come if StopWatch was used outside the "for I" loop. What kind of numbers do you get?

    Also because of the caused function parameter load, compiler optimization might do things. Optimization might "inline" automatic if constants were used
    That brings me to my game and similar issue. I've noticed slight memory leak (which doesn''t have much effect on gameplay, but I'm OCD and don't want it). I have proper destructors for all my classes and at the end of program I .Destroy() them, but do I also have to execute .Free() afterwards?

  3. #13
    if in lazarus then project options>linking> check 'use heaptrc', usefull for tracking memory leaks

  4. #14
    Quote Originally Posted by Darkhog View Post
    I have proper destructors for all my classes and at the end of program I .Destroy() them, but do I also have to execute .Free() afterwards?
    Free is simply a safe way to call Destroy (it checks first if it exists). So no, don't call both. In the implementation:
    Code:
    procedure TObject.Free;
    begin
      // the call via self avoids a warning
      if self<>nil then
        self.destroy;
    end;

  5. #15
    Ah, okay. Thanks for clarification.

  6. #16
    Quote Originally Posted by User137 View Post
    You create TStopWatch 22 times, but never free it. It is memory leak. Also i guess more interesting numbers come if StopWatch was used outside the "for I" loop. What kind of numbers do you get?
    Since TStopWatch isn't a class but is actually a record with lots of class helper methods I'm not creating a memory leak.
    Becouse TStopwatch is record the memory for it gets reserved when entering Mutton1OnClick method. You need to call Stopwatch.StartNow so that initial parameters of StopWatch are being set (automatic chosing of best way for time profiling based on hardware and OS capablitity, resetting timer, etc.).

    Using the StopWatch outside "for I loop" still shows same difference in performance. Now I did find an error in my usage of StopWatch. I was retrieving Milliseconds with ElapsedTime.Milliseconds which returns only milliseconds part but I should have been using ElapsedTime.TotalMilliseconds to retrieve whole elapsed time in Milliseconds.


    Quote Originally Posted by User137 View Post
    Also because of the caused function parameter load, compiler optimization might do things. Optimization might "inline" automatic if constants were used
    Code:
    procedure TWorldData.SetData(const X, Y, Z: Integer; const Position: T3Dposition); //inline; ?
    You can't use that in Getter or Setter property methods.


    Quote Originally Posted by User137 View Post
    Even the GetData allocates and sets 12 bytes for 3 integers for each pos, while nested for-loops do not.
    Booth enumerator and nested for loops aproach retrieve data through the use of property so in both cases GetData is being called. I have written my code specifically to do so in order for me to have realistic comparison.


    Anywhay I managed to improve the overal performance a bit by removing direct typecasting like:
    Code:
    result := TWorldData(FOwner).Data(FXPos,FYPos,FZPos)

    I gues that lower performance I get with my Enumerator implementation is due to using another class which acts as annother abstract layer. Unfortunately you can't do without this aditional abstract level.

  7. #17
    Quote Originally Posted by Darkhog View Post
    @SilverWarrior: IMO, unless you need some record (as in Pascal record type) for each world "tile", enumeration is over engineering it. If nested loops are faster and still do the job, I'd say go for it. Just comment them well so you'll be sure what they do at a later date.
    Don't worry I'll probably still be using nested loops as they also alow you to quickly iterate only through portion of your while data.

    Since the threads question was "How to implement such feature" I went and do a litle research (I have nevere used Enumerations before) and build myself a litle demo to see how hard it is to implement Enumerators and what performance I can get out of them.
    And of course I shared my findings with you guys so you won't have to do all this by yourselves.

  8. #18
    I managed to convert my world data to flat array was easier than I thought -thanks to this thread too. can't really tell if it's faster than before, didn't benchmark

Page 2 of 2 FirstFirst 12

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
  •