2 ms·
I'd say it's quite hard to make C++ approach the speed of Python even with heavy allocation and frequent copying. My ballpark is that Python code will be betwee
by kibwen 4y ago
I'd say it's quite hard to make C++ approach the speed of Python even with heavy allocation and frequent copying. My ballpark is that Python code will be between 10 to 100 times slower than the equivalent C++ or Rust code. Java's an entirely different beast, and can definitely be on par with allocation-heavy C++, though you're not going to be writing Java functions that can be called from within Python. And your intuition about Rust is correct, there's no pressure to defensively copy strings in Rust as there is in C++, you can just safely pass and store the references.
- cyber_kinetist 4y agoYour 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.