regex performance java vs c++11

  • A+

I am learning regex in c++ and java. So i did a performance test on c++11 regex and java regex with same expression and same no of inputs. Strangely java regex is faster than c++11 regex. Is there anything wrong in my code? Pls correct me

Java code:

import java.util.regex.*;  public class Main {     private final static int MAX = 1_000_000;     public static void main(String[] args) {         long start = System.currentTimeMillis();         Pattern p = Pattern.compile("^[//w._]+@//w+//.[a-zA-Z]+$");         for (int i = 0; i < MAX; i++) {             p.matcher("").matches();         }         long end = System.currentTimeMillis();         System.out.print(end-start);     } } 

C++ code:

#include <iostream> #include <Windows.h> #include <regex>  using namespace std;  int main() {     long long start = GetTickCount64();     regex pat("^[//w._]+@//w+//.[a-zA-Z]+$");     for (long i = 0; i < 1000000; i++) {         regex_match("", pat);     }     long long end = GetTickCount64();     cout << end - start;     return 0; } 


Java -> 1003ms C++  -> 124360ms 


As various commenters have pointed out it sounds like you are compiling your C++ code in debug mode which turns off many compiler optimizations and adds some extra diagnostic code to your program.

Since you are using Visual Studio 2017 look for the Solution Configuration drop-down and change it from Debug to Release.

regex performance java vs c++11


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: