2

I just need to know which array is faster : a 2D char array or a 1D array to char pointers.

for example :

char* name1[]={"Marc", "Jean-Marie", "Paul", ...}
char name2[][11]={"Marc", "Jean-Marie", "Paul", ...}

if I have the same exact code that would sort these arrays which one would finish faster ?

6
  • What do you mean by dynamic array? I don't see any dynamic allocation here. Commented Mar 26, 2019 at 4:36
  • Sorry, I meant an array to char pointer, I'll edit that Commented Mar 26, 2019 at 4:40
  • If you're planning on sorting them in place, then name1 would likely be faster as you can swap the pointers. The name2 line would require you to move the entire string, not just the pointers to strings. Commented Mar 26, 2019 at 4:41
  • 3
    Did you try? If you seriously need to speed-optimize something, then there is no way around programming both and measuring. If you only know how to implement one of the two, then go with that first and try whether it really is worth optimizing, seeing that the other one is not necessarily faster. Commented Mar 26, 2019 at 4:44
  • yeah I will do that later, but now I just needed a quick answer. Thank you Commented Mar 26, 2019 at 4:48

2 Answers 2

3

Sorting the second variant will require string copies using an intermediate buffer or byte-for-byte / block-based swapping. It's likely that this will be "slower" than simply moving pointers.

Conversely, using pointers to actual string literals means only swapping pointers as you sort. So it's likely that this will be "faster".

Furthermore, it's likely that your string literals will be packed closer together in memory by the compiler, which can help cache performance, assuming you actually want to sort a much larger number of strings than 3.

For the example you give, however. It's not completely clear-cut. On a system with larger native types (e.g. pointers, or special SIMD enhancements) it's entirely possible that swapping around strings in the 2D array can be optimized to the point where you can barely measure a difference. Although this is highly dependent on the underlying memory architecture, and whether it cares much about alignment.

A final point is that if you have a very large 2D array, it would need to be allocated either statically or on the heap, as it may not fit on the stack.

And of course, you may only begin to see measurable differences at very large array sizes. Using an example with 3 strings is pretty ridiculous.

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

2 Comments

Of course you can allocate a very large 2D array with malloc, even with dynamic dimensions...
Right, of course. This was written in a hurry, addressing these arrays being declared as outlined in the question. I will include this for the sake of completeness.
0

Firstly, for such a small number of elements it really does not matter. Both are equally fast.

However when you have a large number of elements in the array, you need to look at cache access.

For name1 you have an array or pointers. The pointers itself point to some other location where the string literals "Marc" Jean-Marie" etc are stored. This may cause issues with caching the arrays properly.

For name2 the strings "Marc" etc are copied into the array. This may be easier for cache. However, you should note that if you have a large array the second approach will cause more use of RAM.

Comments

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.