Skip to content

[optimization] libc++ std::regex and std::regex_match very slow, ~10x slower than libstdc++ #60991

Open
@raidenluikang

Description

@raidenluikang

I searched for issues but didn't find anything like it.

Let https://godbolt.org/z/o5Wx9x3Gq

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctre.hpp>

#include <string_view>
#include <regex>
#include <chrono>
#include <string>
#include <vector>

bool is_valid_email_ctre (const std::string& email)
{
    return (bool)ctre::match<R"rx((?:(?:[^<>()\[\].,;:\s@"]+(?:\.[^<>()\[\].,;:\s@"]+)*)|".+")@(?:(?:[^<>()\[\].,;:\s@"]+\.)+[^<>()\[\].,;:\s@"]{2,}))rx">(email);
}

bool is_valid_email(const std::string& email) 
{
    static const std::regex rx{R"rx((?:(?:[^<>()\[\].,;:\s@"]+(?:\.[^<>()\[\].,;:\s@"]+)*)|".+")@(?:(?:[^<>()\[\].,;:\s@"]+\.)+[^<>()\[\].,;:\s@"]{2,}))rx"};
    return std::regex_match(email, rx);
}
//......

Clang libc++ output:

STD: less_valid = 1  upp_valid = 1
CTRE: less_valid = 1  upp_valid = 1
STD: valid_count = 4291
STD: elapsed time = 1.147848643
STD: is_valid_email took 11478 nanoseconds
CTRE: valid_count = 4291
CTRE: elapsed time = 0.038445505
CTRE: is_valid_email took 384 nanoseconds

GCC libstdc++ output:

STD: less_valid = 1  upp_valid = 1
CTRE: less_valid = 1  upp_valid = 1
STD: valid_count = 4245
STD: elapsed time = 0.116426089
STD: is_valid_email took 1164 nanoseconds
CTRE: valid_count = 4245
CTRE: elapsed time = 0.010598227
CTRE: is_valid_email took 105 nanoseconds

libc++ STD: elapsed time = 1.147848643 s vs libstdc++ STD elapsed time = 0.116426089, - which 9.86 times slower.

Metadata

Metadata

Assignees

No one assigned

    Labels

    libc++libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.performanceregexIssues related to regex

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions