Jump to content

Leaderboard


Popular Content

Showing content with the highest reputation on 12/26/20 in all areas

  1. Rollo62

    Chess board

    https://www.delphipraxis.net/141566-schach-ki-gesucht.html Some great links in the German DP recently.
  2. Mike Torrettinni

    Example of wasteful, innefficient string manipulation

    Thank you @Kas Ob. I think now I understand my question was really poorly formulated, I was focused on strings manipulation, everybody else thought I was stress testing MM. Makes sense that some of you wanted me to go back to school 🙂 I thought I was becoming better at asking questions, I guess I need more work. Even though it probably seemed like a waste of time for everybody else, I learned valuable things during preparing the example, thinking about it, reading about FastMM and 'discussing' in this topic: 1. I still need to work on improving question quality 2. I'm probably little rebellious when getting suggestions like 'google this and then we can talk' 😉 3. What I thought was very wasteful, bad example of string manipulation, was actually not! 😉 even Delphi 7 handled it really well. If I tested this example in D7 before posting, I would probably realize this is not worth even asking. But it led me to test D7 from references to FastMM - to test version without it. So, something that is probably very basic and very obvious fact for seasoned developers, it wasn't for me and wouldn't come to this conclusion without this topic,
  3. Pawel Piotrowski

    Example of wasteful, innefficient string manipulation

    Actually, your code is wrong. Now you are doubling the memory usage. SetLength creates a big string, then in the loop, you add more to it... If you wish to pre-allocate the length of the string, then you need to do it this way: Function MultiplyStrWithPreallocate(aStr: String; aMultiplier: Integer): String; Var i, pos: Integer; Begin SetLength(Result, Length(aStr) * aMultiplier); pos := 1; For i := 1 To aMultiplier Do Begin move(aStr[1], Result[pos], Length(aStr) * sizeOf(Char)); Inc(pos, Length(aStr)); End; End; Here is what is going on behind the scene, when you call: Result := Result + aStr; the memory manager creates a new temporary string, assigns the content of "result" and "aStr" to it. That new temporary Strings is then assigned to "result" which in turn decreases the reference counter to the String that result is refering... in that case to 0, which causes the memory manager to release that String. So for a short period of time, both, the original "result" and the "result"+"aStr" are in memory. And for each for-loop-iteration, you basically have a copy operation of the whole string. My above optimization using move reduces both of those concerns. There is just one memory allocation, and only a few bytes are copied in each for-loop-iteration
×