How expensive is it to execute
const std::string s(my_const_char_ptr, my_length);
? Is there copying involved? If not, how many instructions can I expect from typical standard library implementations? Few enough to have this in performance-critical code?
... or must I get a GSL implementation and use string_view
?
You need to measure it on your target system and compilation settings if you want to know the exact answer. But for what's happening under the hood:
If my_length
is large enough (or the standard library's std::string
implementation doesn't use small string optimization — which is rare), then there will be a dynamic memory allocation.
In any case, there will be an O(n) character-by-character copy from *my_const_char_ptr
to the std::string
's buffer.