First write something that works, then optimize if needed. When it is, it can be trivially replaced by a better option. Memory allocation is slow in C++ (~80-300 cycles per small object for typical allocators), but fast in Java (< 20 cycles per small object), because Java allocates from continuous memory region - it just increases a pointer.Ĭreating an array to generate a word count is absurd.Īs long as it is unnoticeable to the user, it is not.
#ANOTHER WORD FOR ISSUE BEGINS WITH D CODE#
Don't judge Java code by C++ performance characteristic. Second, for a large string, an additionall pass is much more expensive than additional memory allocation - because of cache issues. substring() used behind the scenes by the split function does not copy anything. Oh really? First - there is no copying in my code. Secondly, three passes over an array is significantly cheaper than memory allocation and copying - something you should know. Counting patterns in data is a very complex subject - again something you should know. But there you have erred on arrogance again: it is not a trivial issue. which is why I originally said four passes.)įinally, you are the one who started this "trivial issue". (Properly formatting it made me notice one more thing that was missing.
Std::not1( std::ptr_fun ( std::isspace ) ) // Otherwise: // number of words := number of whitespace sequences - leading whitespace + trailing whitespace // return ((size_t)std::count_if( s.begin(), s.end(), std::ptr_fun ( std::isspace ) ) = s.length()) If the string contains only whitespace, then there are no words.