-2

I've been trying to learn how to optimize C++ in the context of low latency trading systems, and am wondering if this implementation could be improved. I would appreciate any insight, either specific or general.

// Code to add each word in string to vector
    int main() {
        std::string originalText = "Hello World!";
        std::vector<std::string> words;
        words.reserve(originalText.length()); // unsure if this could be more accurate

        std::size_t wStart = 0;
        std::size_t pos = originalText.find(" ");
        while(pos != std::string::npos) {
            words.emplace_back(&originalText[wStart], pos - wStart);
            wStart = pos + 1;
            pos = originalText.find(" ", wStart);
        }
        words.emplace_back(&originalText[wStart], originalText.size() - wStart);
    
        return 0;
    }
Oreese17
  • 7
  • 1
  • 9
    Does the code *work* (a hard requirement)? Then you want a review and should post on [the code review SE](https://codereview.stackexchange.com/help/on-topic) instead. – Some programmer dude Feb 08 '23 at 03:00
  • 1
    Yeah sure. Probably fine. I guess. Whatever. Profile your code using expected input, find the hotspots, and then work on making those faster. – JohnFilleau Feb 08 '23 at 03:02
  • 4
    In seeking a code review and profiling *before* trying to optimise, it is also essential to describe what the code is intended to do i.e. the original functional requirement. Most techniques to optimise code for performance trade off *something else* and it is not unusual for that to be part of the original functional requirement (e.g. some edge case that is missed or deemed unimportant to the person polishing the code, but is important to the program's end user). Without a description of the original functional requirement, it's impossible to assess if some part of it is being traded off. – Peter Feb 08 '23 at 03:17
  • Note, I think you might have a bug in your code if there is more than once white space between words. Also bugged if there is leading or trailing whitespace at the beginning or end of the `originalText` – selbie Feb 08 '23 at 03:19

1 Answers1

0

You still allocate and copy std::strings from the input that's not needed. You can use string_views. This is no way near optimized for a trading system though. I still use a vector and the will reallocate while it grows. When optimizing code there is two things, keep thinking/researching if there is another algorithm (that usually brings the biggest performance gain). Then keep profiling to find your (real) bottlenecks.

Anyway here is a split function that can split without making string copies and can split on multiple characters (you can still optimize that to only split on spaces if that is the only delimiter)

std::vector<std::string_view> split_string(std::string_view string, std::string_view delimiters)
{
    std::vector<std::string_view> substrings;
    if(delimiters.size() == 0ul)
    {
        substrings.emplace_back(string);
        return substrings;
    }

    auto start_pos = string.find_first_not_of(delimiters);
    auto end_pos = start_pos;
    auto max_length = string.length();

    while(start_pos < max_length)
    {
        end_pos = std::min(max_length, string.find_first_of(delimiters, start_pos));

        if(end_pos != start_pos)
        {
            substrings.emplace_back(&string[start_pos], end_pos - start_pos);
            start_pos = string.find_first_not_of(delimiters, end_pos);
        }
    }

    return substrings;
}
Pepijn Kramer
  • 9,356
  • 2
  • 8
  • 19