小编Edd*_*ddy的帖子

正则表达式性能java vs c ++ 11

我正在学习c ++和java中的正则表达式.所以我对c ++ 11正则表达式和java正则表达式进行了性能测试,表达式相同且输入相同.奇怪的是,java正则表达式比c ++ 11正则表达式更快.我的代码有什么问题吗?请纠正我

Java代码:

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("abcd_ed123.t12y@haha.com").matches();
        }
        long end = System.currentTimeMillis();
        System.out.print(end-start);
    }
}
Run Code Online (Sandbox Code Playgroud)

C++代码:

#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 < …
Run Code Online (Sandbox Code Playgroud)

c++ java regex performance c++11

1
推荐指数
2
解决办法
748
查看次数

标签 统计

c++ ×1

c++11 ×1

java ×1

performance ×1

regex ×1