5 ms·
4) string concatenation
by PixyMisa 6y ago
4) string concatenation
- piker 6y agoProbably (1) above, no?
- pjmlp 6y agoNULL terminated strings are quite bad for performance, because you need to transverse them to find the terminator. Now try to concatenate a bunch of them in C.
- harveywi 6y ago>you need to traverse them to find the terminator. Why not just do a binary search through the entire 256TB virtual memory space, finding the largest memory address that doesn't segfault? :-)
- varajelle 6y agoC++ code usually use std:: string, std::string_view, or other string classes that stores the string size and has some capacity pre-allocated to avoid such issues.
- pjmlp 6y agoThe (1) also mentions C. I would partially agree with you, if it only mentioned C++. Only partially, because too many C++ devs still use plain old C strings.
- zlynx 6y agoProfiling is essential. I found a performance bug in calling some C++ functions a while ago, because they accepted a const std::string& and were being called in loops with a C const char*. Every single call had to construct a std::string involving a strlen, allocation and copy. std::string_view is a nice fix for this but few programmers seem to use it yet.
- astrobe_ 6y agoLike this ? const char* strings[]={"ab","cd", "ef", "gh", ....}; char result[1024*1024*1024*1024]; size_t sz=0; for(int i=0; i<sizeof(strings); i++) { size_t len=strlen(strings[i]); memcpy(result+sz, strings[i], len); sz+=len; } result[sz]=0;