1
votes

I have a Stock class in which one of the members is a string which maximum length will be at most 6 chars (stock tickers in the NASDAQ cannot be longer than 5 characters plus the added Q if they were to go bankrupt). However, since the NASDAQ alone has about 3500 stocks I would like to use as little data as possible when making a Stock object. std::string allocates more space than I need for its contents and I am trying to reduce memory consumption. Would the extra allocated memory that std::string uses affect the performance of my program at all?

In the following code it shows that the member variable symbol has a size of 15 even though there are only 4 chars in it. Can I restrict the string size to 6 bytes using str.reserve inside the class before the initialization, or should I instantiate the object then call symbol.resize(6) to ensure there are only 6 bytes being used?

#include <iostream>
#include <string>

using namespace std;


class Stock
{
public:
    Stock(string s, double o, double c, unsigned int v)
     : symbol(s), open(o), close(c), volume(v) {};
    ~Stock() = default;
    string symbol;
    double open;
    double close;
    unsigned int volume; 
};

int main() {
    Stock AAPL("AAPL", 267.48, 270.71, 26547493);
    cout << AAPL.symbol.capacity() << endl;
    cout << AAPL.symbol.size() << endl;

    return 0;
}

From reading the answer to this question STD::string as a Member Parameter for Dynamically Allocated Objects it seems that this extra allocated space that std::string uses should not interfere at all with the memory of my program, but I am not 100% sure and I would prefer if someone could corroborate my assumption.

1
3500 stocks multiplied by even 100 extra wasted bytes is still only 341KiB. Not worth worrying about in the days of 32GiB RAM, imo. Don’t worry about locality-of-reference for caching either until your benchmarks say otherwise. - Dai
With a ceiling of 6 bytes, just use an array and string_view (C++17) or char* . 6 bytes is less than the size of the pointer to the data. - parktomatomi
Look up "small string optimization" to see how your data is (likely) being stored inside the string object; it's not allocating extra memory beyond that of the base string object. - Shawn
If you REALLY need to optimize for space, store the stock ticker identifier in a singleton vector, and have the transactions use a 16-bit short to index into that vector. - Eljay
I will be adding a date variable as well as two extra double variables for the high and the close, the idea is for a single object to represent a trading day in a particular stock then I can put those objects in a container and write some functions to find chart patterns. Since there will be a lot of objects, I am trying to optimize as much as possible and keep each object tiny. I believe that eventually these extra bytes from std::string might start costing me performance. - Salchipapas

1 Answers

1
votes

I think, Best way is a combination of type definition. Use char ticker[6+1] and if you need to parse the data or process it uses the std::string_view.