Which is faster C++ std::string::length or std::string::size? [duplicate]
Asked Answered
A

4

18

length() returns the number of characters in the string and size() returns a size_t which is also the same but used to make it consistent with other STL containers.

For computing length(), the string iterates through all the characters and counts the length. So, O(n) time.

Is size() also the same ?

Or can size of a variable be computed directly in O(1) time ?

So, my question is, are they the same in terms of speed (as in how they are calculated) or is size computed in O(1) time ?

Adulterant answered 25/7, 2015 at 16:58 Comment(11)
Try running a loop over a set of datas(And calculate the time) you will know which one is fast.Crambo
What makes you think length() is O(n)?Bestiary
O(n) in size of input ? because it will have to iterate over all the characters of the string ?Adulterant
It doesn't have to iterate.Bestiary
So, it just uses the number of bytes required to count the length ?Adulterant
"For computing length(), the string iterates through all the characters and counts the length" - where did you get that idea??? std::string is designed as a string with stored length. It does not have to iterate.Plasma
I thought that might be because in other STL containers like vector etc. that is what has to be done....Adulterant
vector doesn't need to iterate either (nor do most (all?) of the standard containers...)Bestiary
His idea is not so weird. Properly support of UTF-8 in std::string which - oh, my! - does not exist (while char* on *nix platforms is by default utf8-encoded), would indeed require iterating. @Adulterant You can think of std::string simply as of std::vector<char>. length() has O(1) and simply returns m_Length or something like that.Mead
@Oliver: since C++11, size() is guaranteed to be O(1), so yes all containers. This required a change to the glibc implementation of std::list.Blowup
It is faster to type 'size' than 'length' since it has 2 less characters.Cipher
P
36

Both have the same complexity: Constant.

From the N4431 working draft, §21.4.4

size_type size() const noexcept;

Returns: A count of the number of char-like objects currently in the string. Complexity: Constant time.

And

size_type length() const noexcept;

Returns: size().


[...] iterates through all the characters and counts the length [...]

That's C strings you're thinking of.

Proceed answered 25/7, 2015 at 17:6 Comment(0)
K
18

If you take a look at documentation here it says that length and size are the same.

Both string::size and string::length are synonyms and return the same value.

Also if you take a look at the code, length is cached, so the complexity is O(1). (Code from MS implementation but I'm sure other libraries are done the same way.)

size_type length() const _NOEXCEPT
    {   // return length of sequence
    return (this->_Mysize);
    }

size_type size() const _NOEXCEPT
    {   // return length of sequence
    return (this->_Mysize);
    }
Ketosis answered 25/7, 2015 at 17:8 Comment(0)
B
2

Both the functions for a string in C++ are exactly the same and Returns the number of characters in the string, not including any null-termination.

source

Source: basic_string.h

Balder answered 22/3, 2021 at 15:45 Comment(0)
M
0

They are equivalent. Also, strings don't count characters when returning size, that's char arrays, if strings always counted characters then they would be too slow.

Mendicant answered 9/1, 2017 at 13:37 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.