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;








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 ?










share|improve this question
























  • 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. 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






  • 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

















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 ?










share|improve this question
























  • 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. 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






  • 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













2












2








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 ?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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, 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






  • 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











  • 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












  • 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












2 Answers
2






active

oldest

votes


















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.






share|improve this answer

























  • 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


















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.






share|improve this answer

























    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
    );



    );













    draft saved

    draft discarded


















    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









    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.






    share|improve this answer

























    • 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















    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.






    share|improve this answer

























    • 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













    3












    3








    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.






    share|improve this answer















    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.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    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 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

















    • 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
















    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













    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.






    share|improve this answer



























      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.






      share|improve this answer

























        0












        0








        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        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



























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현