3

I am not a Delphi expert and I was reading online about dynamic arrays and static arrays. In this article I have found a chapter called "Dynamic v. Static Arrays" with a code snippet and below the author says:

[...] access to a dynamic array can be faster than a static array!

I have understood that dynamic arrays are located on the heap (they are implemented with references/pointers).

So far I know that the access time is better on dynamic arrays. But is that the same thing with the allocation? Like if I called SetLength(MyDynArray, 5) is that slower than creating a MyArray = array[0..4] of XXX?

18
  • 2
    The test application you linked to is bogus. The static array there is declared as a global variable so it exists in the application's data segment rather than on the stack. The linked performance test is ridiculous - it doesn't do anything to eliminate other sources of latency. Simply repeating or reversing the order of the tests changes the result. The author doesn't have a clue. Commented Jul 5, 2017 at 13:32
  • 1
    Also, please beware, that article is comparing 16bit and 32bit!!! 16 bit does not even exist anymore!!! The author is also calling something RTTI which is not really RTTI. So personally, I don't trust that article. Commented Jul 5, 2017 at 13:37
  • 3
    @EmmaRossignoli No, dynamic arrays do not have faster access at all. The article is wrong, the test he performed was bad, and the conclusions were fallacious. Commented Jul 5, 2017 at 13:42
  • 3
    @DavidHeffernan Trust is not required, surely. It's entirely a caching phenomenon. In the quoted test the dynamic array was freshly allocated (and living in cache). The static array had to be fetched from memory into cache since it was a global var and not living on the stack (which would naturally be cached). Repeating the static array test subsequently (once it has been cached) returns equal performance to the dynamic array. Commented Jul 5, 2017 at 13:45
  • 2
    @Jerry The article is quite old now and Dr Bob is something of a celebrity Commented Jul 5, 2017 at 16:46

1 Answer 1

14

So far I know that the access time is better on dynamic arrays.

That is not correct. The statement in that article is simply false.

But is that the same thing with the allocation? Like if I called SetLength(MyDynArray, 5) is that slower than creating a MyArray = array[0..4] of XXX?

A common fallacy is that static arrays are allocated on the heap. They could be global variables, and so allocated automatically when the module is loaded. They could be local variables and allocated on the stack. They could be dynamically allocated with calls to New or GetMem. Or they could be contained in a compound type (e.g. a record or a class) and so allocated in whatever way the owning object is allocated.

Having got that clear, let's consider a couple of common cases.

Local variable, static array type

As mentioned, static arrays declared as local variables are allocated on the stack. Allocation is automatic and essentially free. Think of the allocation as being performed by the compiler (when it generates code to reserve a stack frame). As such there is no runtime cost to the allocation. There may be a runtime cost to access because this might generate a page fault. That's all perfectly normal though, and if you want to use a small fixed size array as a local variable then there is no faster way to do it.

Member variable of a class, static array type

Again, as described above, the allocation is performed by the containing object. The static array is part of the space reserved for the object and when the object is instantiated sufficient memory is allocated on the heap. The cost for heap allocation does not typically depend significantly on the size of the block to be allocated. An exception to that statement might be really huge blocks but I'm assuming your array is relatively small in size, tens or hundreds of bytes. Armed with that knowledge we can see again that the cost for allocation is essentially zero, given that we are already allocating the memory for the containing object.

Local variable, dynamic array type

A dynamic array is represented by a pointer. So your local variable is a pointer allocated on the stack. The same argument applies as for any other local variable, for instance the local variable of static array type discussed above. The allocation is essentially free. Before you can do anything with this variable though, you need to allocate it with a call to SetLength. That incurs a heap allocation which is expensive. Likewise when you are done you have to deallocate.

Member variable of a class, dynamic array type

Again, allocation of the dynamic array pointer is free, but you must call SetLength to allocate. That's a heap allocation. There needs to be a deallocation too when the object is destroyed.

Conclusion

For small arrays, whose lengths are known at compile time, use of static arrays results in more efficient allocation and deallocation.

Note that I am only considering allocation here. If allocation is a relatively insignificant portion of the time spent working with the object then this performance characteristic may not matter. For instance, suppose the array is allocated at program startup, and then used repeatedly for the duration of the program. In such a scenario the access times dominate the allocation times and the difference between allocation times becomes insignificant.

On the flip side, imagine a short function called repeatedly during the programs lifetime, let's suppose this function is the performance bottleneck. If it operates on a small array, then it is possible that the allocation cost of using a dynamic array could be significant.

Very seldom can you draw hard and fast rules with performance. You need to understand how the tools work, and understand how your program uses these tools. You can then form opinions on which coding strategies might perform best, opinions that you should then test by profiling. You will be surprised more often than you might expect that your intuition is not a good predictor of performance.

Sign up to request clarification or add additional context in comments.

1 Comment

That last paragraph is the most crucial one to understand. While the rest answers the question, the last should be reconsidered every time one considers refactoring for performance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.