Recent

Author Topic: [Solved] Dynamic arrays much slower than GetMem  (Read 2809 times)

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 911
Re: Dynamic arrays much slower than GetMem
« Reply #15 on: September 27, 2021, 08:10:42 pm »
The attached project computes a gaussian blur on a 3D volume of dimensions kDim^3. With kDim = 256, the exact same computations are 172 times slower (Apple M1, 1394 vs 239723 ms) and 294 times slower (Ryzen 3900X, 2706 vs 796013 ms ) than using traditional arrays.  I realize that there is some overhead to calling setlength(), where the array will be initialized as zeros, but there are very few calls to setlengths().
In case of a dynamic array, there's (on 64 bit platforms) a 16 byte header at the start of the memory block (containing the reference count and size), but I'm not sure how that could explain such a difference in speed.

ASerge

  • Hero Member
  • *****
  • Posts: 1869
Re: Dynamic arrays much slower than GetMem
« Reply #16 on: September 27, 2021, 09:08:16 pm »
Maybe I didn't make it clear in the previous message, but the program compares different algorithms! When I made the algorithms the same, the dynamic version became even faster.

ChrisR

  • Full Member
  • ***
  • Posts: 227
Re: Dynamic arrays much slower than GetMem
« Reply #17 on: September 27, 2021, 10:03:16 pm »
ASerge

I agree that the difference in speed can be resolved by changing the dynamic array call from
 tmp := copy(img, imgp, nx);
to
  Move(img^[imgp], tmp^[0], nx * 4);

Even though the array has the same size before and after the copy(), there seems to be a big penalty.


Kays

  • Sr. Member
  • ****
  • Posts: 367
  • Whasup!?
    • KaiBurghardt.de
Re: Dynamic arrays much slower than GetMem
« Reply #18 on: September 27, 2021, 11:06:27 pm »
[…] I have used dynamic arrays extensively in my projects as they are convenient. […]
Dynamic arrays are managed data types. The FPC will automagically insert try … finally frames when you use them. You can [but shouldn’t] control this with {$implicitExceptions}. For more on that topic cf. avoiding implicit try finally section in the Wiki.
Yours Sincerely
Kai Burghardt

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 911
Re: Dynamic arrays much slower than GetMem
« Reply #19 on: September 28, 2021, 08:41:13 am »
ASerge

I agree that the difference in speed can be resolved by changing the dynamic array call from
 tmp := copy(img, imgp, nx);
to
  Move(img^[imgp], tmp^[0], nx * 4);

Even though the array has the same size before and after the copy(), there seems to be a big penalty.
copy always allocates a new array.

ASerge

  • Hero Member
  • *****
  • Posts: 1869
Re: Dynamic arrays much slower than GetMem
« Reply #20 on: September 28, 2021, 06:27:30 pm »
I agree that the difference in speed can be resolved by changing the dynamic array call from
 tmp := copy(img, imgp, nx);
to
  Move(img^[imgp], tmp^[0], nx * 4);
Again, you did not understand me. The problem is that the functions with a dynamic and static array are different. That is, they do not the same thing. That is, it is useless to compare them, no matter what tricks you do with Move. They are different!
In my first message, I just showed where the error is located, and pointed out that if make them logically the same, then the difference in speed disappears.
Was
Code: Pascal  [Select][+][-]
  1. tmp := copy(img, imgp, imgp + nx - 1);
After correction (this is similar to the version without a dynamic array)
Code: Pascal  [Select][+][-]
  1. tmp := copy(img, imgp, nx);
Imagine that imgp equal 100000 to understand where the error is, and why there is such a difference in speed.

ChrisR

  • Full Member
  • ***
  • Posts: 227
Re: Dynamic arrays much slower than GetMem
« Reply #21 on: September 29, 2021, 01:45:16 pm »
ASerge is correct, my original problem was that the copy() command was copying a larger array than required. So while there copy() command may have a slight penalty versus move(), the effect is small.


So this thread is largely a false alarm. It does look like dynamic array can perform reasonably as long as you minimize the use of setlength() calls (which carry some overhead and obligatorily zero arrays).

My fault entirely. Thanks for the community help.

By the way, how do I set a thread to being "solved" with this forum?

dseligo

  • Sr. Member
  • ****
  • Posts: 360
Re: Dynamic arrays much slower than GetMem
« Reply #22 on: September 29, 2021, 01:56:16 pm »
By the way, how do I set a thread to being "solved" with this forum?

Just change title of the first post to: '[SOLVED] Dynamic arrays much slower than GetMem'

 

TinyPortal © 2005-2018