hanickadot / compile-time-regular-expressions

Compile Time Regular Expression in C++
https://twitter.com/hankadusikova
Apache License 2.0
3.22k stars 177 forks source link

[optimization] CTRE slower than boost regex for validate emails #278

Open raidenluikang opened 1 year ago

raidenluikang commented 1 year ago

CTRE: latest main branch current moment.

for validate email I use: (?:(?:[^<>()\[\].,;:\s@"]+(?:\.[^<>()\[\].,;:\s@"]+)*)|".+")@(?:(?:[^<>()\[\].,;:\s@"]+\.)+[^<>()\[\].,;:\s@"]{2,}) regex.

Let attached file (extension changed to LOG, because can't attach .cpp file): example.LOG

Build: $ clang++ -o example example.cpp -O2 -std=c++20 -stdlib=libc++ -Wall -Wextra -Werror -Wpedantic

Result in my PC i5-11400, 16GB RAM ubuntu 20.04 Clang 15.07:

$ ./example 
STD: less_valid = 1  upp_valid = 1
BOOST: less_valid = 1  upp_valid = 1
CTRE: less_valid = 1  upp_valid = 1
====================================================
STD: valid_count = 21907
STD: elapsed time = 2.431440970
STD: is_valid_email took 2431 nanoseconds
=====================================================
BOOST: valid_count = 21907
BOOST: elapsed time = 0.361886186
BOOST: is_valid_email took 361 nanoseconds
=====================================================
CTRE: valid_count = 21907
CTRE: elapsed time = 1.335482155
CTRE: is_valid_email took 1335 nanoseconds
====================================================
ME: valid_count = 21907
ME: elapsed time = 0.042453908
ME: is_valid_email took 42 nanoseconds

CTRE took 1335 ns per call is_valid_email_ctre, boost took 361 ns, which more than 3 times faster CTRE's.

Hint: CTRE become slower when I added quoted symbols to test emails data.


Result when built with g++11.0:

$ ./example
STD: less_valid = 1  upp_valid = 1
BOOST: less_valid = 1  upp_valid = 1
CTRE: less_valid = 1  upp_valid = 1
====================================================
STD: valid_count = 22071
STD: elapsed time = 1.210953864
STD: is_valid_email took 1210 nanoseconds
=====================================================
BOOST: valid_count = 22071
BOOST: elapsed time = 0.354756538
BOOST: is_valid_email took 354 nanoseconds
=====================================================
CTRE: valid_count = 22071
CTRE: elapsed time = 0.614597561
CTRE: is_valid_email took 614 nanoseconds
====================================================
ME: valid_count = 22071
ME: elapsed time = 0.039021986
ME: is_valid_email took 39 nanoseconds

Now CTRE ~2 times slower than Boost regex.

raidenluikang commented 1 year ago

Seems CTRE slower when check "\.+" part of regex.