which is faster a 2D char array or a 1D array to char pointers?Create ArrayList from arrayHow do I check if an array includes an object in JavaScript?How to append something to an array?Deleting an element from an array in PHPImprove INSERT-per-second performance of SQLite?Loop through an array in JavaScriptHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?Why are elementwise additions much faster in separate loops than in a combined loop?For-each over an array in JavaScript?
What's a moment that's more impactful on a reread called?
Why are they 'nude photos'?
I quit, and boss offered me 3 month "grace period" where I could still come back
Are there any double stars that I can actually see orbit each other?
Is Trump personally blocking people on Twitter?
Supporting developers who insist on using their pet language
Reverse the word order in string, without reversing the words
Correct use of ergeben?
Do native speakers use ZVE or CPU?
Why does resistance reduce when a conductive fabric is stretched?
Can I intentionally omit previous work experience or pretend it doesn't exist when applying for jobs?
<schwitz>, <zwinker> etc. Does German always use 2nd Person Singular Imperative verbs for emoticons? If so, why?
What's the minimum number of sensors for a hobby GPS waypoint-following UAV?
How can an advanced civilization forget how to manufacture its technology?
How the name "craqueuhhe" is read
Repeating redundant information after dialogues, to avoid or not?
Cops: The Hidden OEIS Substring
Bronze Age Underwater Civilization
What would be the ideal melee weapon made of "Phase Metal"?
QGIS Welcome page: What is 'pin to list' for?
Are neural networks prone to catastrophic forgetting?
Why are Hobbits so fond of mushrooms?
What's the point of this scene involving Flash Thompson at the airport?
Keep milk (or milk alternative) for a day without a fridge
which is faster a 2D char array or a 1D array to char pointers?
Create ArrayList from arrayHow do I check if an array includes an object in JavaScript?How to append something to an array?Deleting an element from an array in PHPImprove INSERT-per-second performance of SQLite?Loop through an array in JavaScriptHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?Why are elementwise additions much faster in separate loops than in a combined loop?For-each over an array in JavaScript?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
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 ?
c arrays dynamic
|
show 2 more comments
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 ?
c arrays dynamic
What do you mean by dynamic array? I don't see any dynamic allocation here.
– Thomas Jager
Mar 26 at 4:36
Sorry, I meant an array to char pointer, I'll edit that
– PotatoGrill
Mar 26 at 4:40
If you're planning on sorting them in place, thenname1
would likely be faster as you can swap the pointers. Thename2
line would require you to move the entire string, not just the pointers to strings.
– Thomas Jager
Mar 26 at 4:41
okay thank you !
– PotatoGrill
Mar 26 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.
– Yunnosch
Mar 26 at 4:44
|
show 2 more comments
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 ?
c arrays dynamic
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 ?
c arrays dynamic
c arrays dynamic
edited Mar 26 at 4:54
PotatoGrill
asked Mar 26 at 4:32
PotatoGrillPotatoGrill
153 bronze badges
153 bronze badges
What do you mean by dynamic array? I don't see any dynamic allocation here.
– Thomas Jager
Mar 26 at 4:36
Sorry, I meant an array to char pointer, I'll edit that
– PotatoGrill
Mar 26 at 4:40
If you're planning on sorting them in place, thenname1
would likely be faster as you can swap the pointers. Thename2
line would require you to move the entire string, not just the pointers to strings.
– Thomas Jager
Mar 26 at 4:41
okay thank you !
– PotatoGrill
Mar 26 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.
– Yunnosch
Mar 26 at 4:44
|
show 2 more comments
What do you mean by dynamic array? I don't see any dynamic allocation here.
– Thomas Jager
Mar 26 at 4:36
Sorry, I meant an array to char pointer, I'll edit that
– PotatoGrill
Mar 26 at 4:40
If you're planning on sorting them in place, thenname1
would likely be faster as you can swap the pointers. Thename2
line would require you to move the entire string, not just the pointers to strings.
– Thomas Jager
Mar 26 at 4:41
okay thank you !
– PotatoGrill
Mar 26 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.
– Yunnosch
Mar 26 at 4:44
What do you mean by dynamic array? I don't see any dynamic allocation here.
– Thomas Jager
Mar 26 at 4:36
What do you mean by dynamic array? I don't see any dynamic allocation here.
– Thomas Jager
Mar 26 at 4:36
Sorry, I meant an array to char pointer, I'll edit that
– PotatoGrill
Mar 26 at 4:40
Sorry, I meant an array to char pointer, I'll edit that
– PotatoGrill
Mar 26 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.– Thomas Jager
Mar 26 at 4:41
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.– Thomas Jager
Mar 26 at 4:41
okay thank you !
– PotatoGrill
Mar 26 at 4:41
okay thank you !
– PotatoGrill
Mar 26 at 4:41
3
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.
– Yunnosch
Mar 26 at 4:44
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.
– Yunnosch
Mar 26 at 4:44
|
show 2 more comments
2 Answers
2
active
oldest
votes
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.
Of course you can allocate a very large 2D array withmalloc
, even with dynamic dimensions...
– Antti Haapala
Mar 26 at 6:29
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.
– paddy
Mar 26 at 6:36
add a comment |
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.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55349898%2fwhich-is-faster-a-2d-char-array-or-a-1d-array-to-char-pointers%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Of course you can allocate a very large 2D array withmalloc
, even with dynamic dimensions...
– Antti Haapala
Mar 26 at 6:29
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.
– paddy
Mar 26 at 6:36
add a comment |
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.
Of course you can allocate a very large 2D array withmalloc
, even with dynamic dimensions...
– Antti Haapala
Mar 26 at 6:29
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.
– paddy
Mar 26 at 6:36
add a comment |
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.
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.
edited Mar 26 at 6:37
answered Mar 26 at 4:48
paddypaddy
44.6k5 gold badges39 silver badges79 bronze badges
44.6k5 gold badges39 silver badges79 bronze badges
Of course you can allocate a very large 2D array withmalloc
, even with dynamic dimensions...
– Antti Haapala
Mar 26 at 6:29
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.
– paddy
Mar 26 at 6:36
add a comment |
Of course you can allocate a very large 2D array withmalloc
, even with dynamic dimensions...
– Antti Haapala
Mar 26 at 6:29
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.
– paddy
Mar 26 at 6:36
Of course you can allocate a very large 2D array with
malloc
, even with dynamic dimensions...– Antti Haapala
Mar 26 at 6:29
Of course you can allocate a very large 2D array with
malloc
, even with dynamic dimensions...– Antti Haapala
Mar 26 at 6:29
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.
– paddy
Mar 26 at 6:36
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.
– paddy
Mar 26 at 6:36
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Mar 26 at 4:49
Rishikesh RajeRishikesh Raje
6,3432 gold badges10 silver badges28 bronze badges
6,3432 gold badges10 silver badges28 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55349898%2fwhich-is-faster-a-2d-char-array-or-a-1d-array-to-char-pointers%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What do you mean by dynamic array? I don't see any dynamic allocation here.
– Thomas Jager
Mar 26 at 4:36
Sorry, I meant an array to char pointer, I'll edit that
– PotatoGrill
Mar 26 at 4:40
If you're planning on sorting them in place, then
name1
would likely be faster as you can swap the pointers. Thename2
line would require you to move the entire string, not just the pointers to strings.– Thomas Jager
Mar 26 at 4:41
okay thank you !
– PotatoGrill
Mar 26 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.
– Yunnosch
Mar 26 at 4:44