为什么超前(有时)比捕获快? [英] Why is lookahead (sometimes) faster than capturing?

查看:112
本文介绍了为什么超前(有时)比捕获快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题的灵感来自s/,(\d)/$1/s/,(?=\d)//进行比较:前者使用捕获组仅替换数字,而不替换逗号,后者使用前瞻性确定逗号是否由数字代替.如此答案中所讨论的,为什么有时后者有时会更快?

Comparing s/,(\d)/$1/ to s/,(?=\d)//: the former uses a capture group to replace only the digit but not the comma, the latter uses a lookahead to determine whether the comma is succeeded by a digit. Why is the latter sometimes faster, as discussed in this answer?

推荐答案

这两种方法做不同的事情,并且开销成本也不同.捕获时,perl必须复制捕获的文本.无需消耗即可进行前瞻性比赛;它必须标记开始的位置.您可以使用re 'debug'编译指示来查看正在发生的事情:

The two approaches do different things and have different kinds of overhead costs. When you capture, perl has to make a copy of the captured text. Look-ahead matches without consuming; it has to mark the location where it starts. You can see what's happening by using the re 'debug' pragma:

use re 'debug';
my $capture = qr/,(\d)/;


Compiling REx ",(\d)"
Final program:
   1: EXACT  (3)
   3: OPEN1 (5)
   5:   DIGIT (6)
   6: CLOSE1 (8)
   8: END (0)
anchored "," at 0 (checking anchored) minlen 2 
Freeing REx: ",(\d)"

use re 'debug';
my $lookahead = qr/,(?=\d)/;


Compiling REx ",(?=\d)"
Final program:
   1: EXACT  (3)
   3: IFMATCH[0] (8)
   5:   DIGIT (6)
   6:   SUCCEED (0)
   7: TAIL (8)
   8: END (0)
anchored "," at 0 (checking anchored) minlen 1 
Freeing REx: ",(?=\d)"

我希望在大多数情况下,提前查找比捕获要快,但是正如其他线程所指出的,正则表达式的性能可能取决于数据.

I'd expect look-ahead to be faster than capturing in most cases, but as noted in the other thread regex performance can be data dependent.

这篇关于为什么超前(有时)比捕获快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆