Why is compiling with <regex> header slower than average?

I'm using g++ (GCC) 7.2.1 20171128 compiler on a GNU/Linux system, and I've noticed that compiling a simple file with <regex> header included takes noticably longer than average.

Example, without <regex>:

1
2
3
4
5
6
7
8
9
#include <iostream>
#include <string>

int main()
{
        std::string s1("1234");
            
        return 0;
}


time make
g++ -std=c++11 main.cpp

real 0m0.485s
user 0m0.451s
sys 0m0.034s



With <regex>:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>
#include <regex>

int main()
{
        std::string s1("1234");            
        std::regex pat{"(^[0-9]+[.]?[0-9]*$)|(^[.][0-9]+$)"};
        std::cout << std::regex_match(s1, pat) << std::endl;

        return 0;
}


time make
g++ -std=c++11 main.cpp

real 0m4.172s
user 0m3.961s
sys 0m0.209s


The difference is almost ten-fold! I've tested it with simpler regex expressions in the constructor of the regex object, but it still takes about the same time.

Can anyone explain to me why does compiling <regex> take so long? Are regular expressions really that complicated?
Registered users can post here. Sign in or register to post.