std::string::assign vs std::string::operator=

12,766

Solution 1

Both are equally fast, but = "..." is clearer.

If you really want fast though, use assign and specify the size:

test2.assign("Hello again", sizeof("Hello again") - 1); // don't copy the null terminator!
// or
test2.assign("Hello again", 11);

That way, only one allocation is needed. (You could also .reserve() enough memory beforehand to get the same effect.)

Solution 2

I tried benchmarking both the ways.

static void string_assign_method(benchmark::State& state) {
  std::string str;
  std::string base="123456789";  
  // Code inside this loop is measured repeatedly
  for (auto _ : state) {
    str.assign(base, 9);
  }
}
// Register the function as a benchmark
BENCHMARK(string_assign_method);

static void string_assign_operator(benchmark::State& state) {
  std::string str;
  std::string base="123456789";   
  // Code before the loop is not measured
  for (auto _ : state) {
    str = base;
  }
}
BENCHMARK(string_assign_operator);

Here is the graphical comparitive solution. It seems like both the methods are equally faster. The assignment operator has better results.

Use string::assign only if a specific position from the base string has to be assigned.

Share:
12,766
malarres
Author by

malarres

Updated on June 03, 2022

Comments

  • malarres
    malarres almost 2 years

    I coded in Borland C++ ages ago, and now I'm trying to understand the "new"(to me) C+11 (I know, we're in 2015, there's a c+14 ... but I'm working on an C++11 project)

    Now I have several ways to assign a value to a string.

    #include <iostream>
    #include <string>
    int main ()
    {
      std::string test1;
      std::string test2;
      test1 = "Hello World";
      test2.assign("Hello again");
    
      std::cout << test1 << std::endl << test2;
      return 0;
    }
    

    They both work. I learned from http://www.cplusplus.com/reference/string/string/assign/ that there are another ways to use assign . But for simple string assignment, which one is better? I have to fill 100+ structs with 8 std:string each, and I'm looking for the fastest mechanism (I don't care about memory, unless there's a big difference)

  • Kilian Brendel
    Kilian Brendel over 5 years
    Be careful not to include the trailing \0 from the C string in the std::string, this will bite you if you later try to append to it. In the example above, use a length of 11, not 12.
  • emlai
    emlai over 5 years
    @KilianRosbach Oops, you're right of course. Fixed now.
  • malarres
    malarres over 4 years
    Thanks. I didn't know quick-bench, it looks like a great tool.For c++ 11 it's curious to see that depending on the compiler (clang, gcc) either one or the other is faster.
  • Boanerges
    Boanerges over 4 years
    Yes! It enables one to take a quick decision of which method to use. You can also benchmark more than 2 pieces of code at the same time.
  • 137
    137 about 3 years
    There's a slight problem with the string_assign_method. It's calling the incorrect overload of std::string::assign. You are copying an empty string essentially. You should instead do: str.assign(base, 0, 9);. Benchmark results are unaffected though