string storage RRS feed

  • Question

  • I'm needing to pile up an unknown number of fixed length strings of data (34 characters each) and am currently just adding to the end of a std::string.At some points this string could get piled as high as several thousand, making the std::string over maybe as much as several thousand characters long.  Would I improve the speed or performance by turning it into an array of std::strings instead and putting a limit on it beforehand (say std::string[67000] or so)  ? 

    I'm not using a database program for good reasons, and all the data in the string is also stored to a binary file; but I keep the string in memory for quick access to any part of it...

    Thursday, August 16, 2018 9:26 PM


  • If the strings are fixed at 34 characters, I think you could avoid a lot of unnecessary overhead associated with std::string by using a two-dimensional array of char (either [x][34] or [x][35] if an extra terminating nul is needed).  If you up the array size to 64, your compiler can probably generate the indexing code with a shift (of 6) instead of a multiply.  (I don't know if any compilers simulate multiplying by 34 with two shifts (of 5 and 1) followed by an add.)

    You could also avoid consuming too much "stack" space by using dynamic allocation (new or malloc) with a pointer of type (*)[34].

    • Marked as answer by TallGuy63 Friday, August 17, 2018 1:05 AM
    Thursday, August 16, 2018 10:44 PM