lundi 25 novembre 2019

Does comparing c++ strings via default copy constructor impact performance and why?

I am trying to understand the performance implication of std::string copy constructor when used for string comparison. Here is the code I wrote for test

// Example program
#include <iostream>
#include <string>
#include <vector>
#include <chrono>
#include <sstream>
#include <algorithm>
#include <iterator>

void function1(std::string& v1)
{
    std::string result;
    std::string v2 = v1;
    for(int i = 0; i<v1.size(); ++i)
    {
        if(v1[i] == v2[i])
        {
            result="function1: Elements are the same for " + std::to_string(i) + " times";
        }
    }
    std::cout<<result<<'\n';
}

void function2(std::string& v1)
{
    std::string result;
    std::string v2 = v1;
    for(int i = 0; i < v1.size(); ++i)
    {
        if(std::string(v1[i],1) == std::string(v2[i],1))
        {
            result="function2: Elements are the same for " + std::to_string(i) + " times";
        }
    }
    std::cout<<result<<'\n';
}

int main()
{
    using namespace std;
  string longString = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
  "Vestibulum at velit in erat hendrerit venenatis at nec lorem. Etiam ante justo, "
  "finibus vel erat ut, porttitor vestibulum diam. Aenean a mauris id ante porta pulvinar. "
  "Mauris est est, lacinia a placerat ut, eleifend quis nisl. Ut posuere ultrices interdum. "
  "Morbi tristique elit quis nibh pharetra, vel aliquam libero iaculis. Suspendisse tempus,"
  "orci non volutpat hendrerit, metus ex feugiat risus, id vehicula risus velit eu elit. "
  "Mauris iaculis vehicula turpis, vitae tempor leo elementum id. Maecenas rhoncus ex quis" 
  "nulla tincidunt consectetur. Sed sit amet lectus tempor, egestas libero quis, viverra massa. C++";

  std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
  function1(longString);
  std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
  std::cout << "Time difference = " << std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count() << "[µs]" << std::endl;
  std::cout << "Time difference = " << std::chrono::duration_cast<std::chrono::nanoseconds> (end - begin).count() << "[ns]" << std::endl;
  std::cout << "------------------    " << std::endl;
  begin = std::chrono::steady_clock::now();
  function2(longString);
  end = std::chrono::steady_clock::now();
  std::cout << "Time difference = " << std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count() << "[µs]" << std::endl;
  std::cout << "Time difference = " << std::chrono::duration_cast<std::chrono::nanoseconds> (end - begin).count() << "[ns]" << std::endl;
}

On my local machine I am getting the following result,

function1: Elements are the same for 670 times Time difference = 395[µs]

Time difference = 395565[ns]

function2: Elements are the same for 670 times Time difference = 475[µs] Time difference = 475549[ns]

Why is there a time difference? Does the std::string copy constructor contribute to this performance difference and why ?

Aucun commentaire:

Enregistrer un commentaire