3 ms·
Your C++ code can even be much slower than Python if you are not careful. Here's a good example of a StackOverflow question asking why it takes a longer time to
by cyber_kinetist 4y ago
Your C++ code can even be much slower than Python if you are not careful. Here's a good example of a StackOverflow question asking why it takes a longer time to split a string in C++ than in Python:
https://stackoverflow.com/questions/9378500/why-is-splitting-a-string-slower-in-c-than-python https://stackoverflow.com/questions/9378500/why-is-splitting...
Python's objects are ref-counted and stored on the heap, so copies do not happen unless you make it explicit. And Python's strings are all interned and stored in a single hashtable, so duplicate strings will not consume any more memory. These properties makes Python quite competitive with naively written C++/Rust. You can certainly write fast string in C++/Rust using string slices (std::string_view in C++ and &str in Rust), but in C++ you need to worry about any memory-related errors and in Rust you need to fight with the borrow checker. I also think that if you really want the best performance in C++/Rust then you need to write your own string class tailor-made to your problem.