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. There is something 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++) {
System.out.println(p.matcher("abcd_ed123.t12y@haha.com").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++) {
cout << regex_match("abcd_ed123.t12y@haha.com", pat) << endl;
}
long long end = GetTickCount64();
cout << end - start;
return 0;
}
Performance:
Java -> 1003ms
C++ -> 124360ms
Aucun commentaire:
Enregistrer un commentaire