deniz
deniz

Reputation: 2577

Performance comparison: strstr() vs std::string::find()

Can someone please explain why I should use strstr or string find() ? Which one is faster, at where ?

Upvotes: 4

Views: 12265

Answers (4)

Ojotuno
Ojotuno

Reputation: 25

#include <chrono>
#include <iostream>
#include <cstring>

void strstr()
{
 auto const start = std::chrono::system_clock::now();
 const char* strsource = "Lorem Ipsum is simplystd::chrono::system_clock::now() dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
 for (int i = 0; i < 1000000; ++i) {
    std::strstr(strsource, "Aldus");
 }
 auto end = std::chrono::system_clock::now();
 std::cout << "strstr microsecs: "<< std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
}

void find()
{
auto start = std::chrono::system_clock::now();
 const std::string strsource = "Lorem Ipsum is simplystd::chrono::system_clock::now() dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
 for (int i = 0; i < 1000000; ++i) {
    (void)strsource.find("Aldus");
 }
 auto end = std::chrono::system_clock::now();
 std::cout << "std::string::find microsecs: "<< std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
}

int main() 
{
    strstr();
    std::cout << std::endl;
    find();
    return 0;
}   

If you run this code in compiler explorer you will see that strstr is way faster than string::find function BUT for this particular case. I think this function is very dedependant of the position and the length of the subtstring you are searching for.

strstr microsecs: 2308

std::string::find microsecs: 13327

gcc 13.2 -O0 -std=c++17

Upvotes: 0

Yuri Borisov
Yuri Borisov

Reputation: 41

I've benchmarked several versions of searching substring in string.

GCC 10.3 with std=c++20/-O3 shows the following results:

  • strstr is the fastest
  • std::string::find ~3 times slower
  • std::search ~4.5 times slower
  • std::string_view::find ~ 11 times slower

Clang 12:

  • strstr is the fastest
  • std::string::find ~3 times slower
  • std::search ~5.5 times slower
  • std::string_view::find ~ 8.5 times slower

So if you do care about performance use strstr.

Upvotes: 4

AlexTheo
AlexTheo

Reputation: 4184

It doesn't matter which is faster. Much more important is that the std::string::find is safe. So use std::string class and avoid old c functions, if you are going to use c++.

Upvotes: 9

Dirk Holsopple
Dirk Holsopple

Reputation: 8831

In C++ you should use std::string::find(), in C you should use strstr(). The difference in performance should not be significant.

Upvotes: 17

Related Questions