English 中文(简体)
如何改善 用非常大的搜索和替换变量进行常规的性能?
原标题:How to improve Perl regex performance with very large search and replace variables?
  • 时间:2012-01-12 05:57:28
  •  标签:
  • perl

在Perl搜索中,用大型变量取代,需要很长时间。

例如。

$original =  aaaabc ;
$replace =  bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb  x 1000
$original =~ s/b/$replace/;

一旦<代码> 替换的大小便足够大,该编码可占用很长时间。 我假定一些缓冲被破坏,并不断延伸。

是否有改进业绩的建议?

问题回答

规模如何大? 替换是在我的视窗盒上的第二座,即使有一段长度30 000 00030 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000美元:

> perl -Mstrict -wE "my $start = time;my $str =  aaaabc ; my $replace =  b  x 30_000_000_000_000_000_000; $str =~ s/b/$replace/; printf qq<%d s
>, time - $start;"
0 s

不清楚为什么你看到业绩恶化。 我为取代500+特性做了扼杀,然后以书面方式执行你的方案。

$ time(perl large.pl )

real    0m0.010s
user    0m0.002s
sys     0m0.004s
$ 

然而,我确实有建议。 如果你更换舱位的长度与你相同,那么为什么在你最初的舱位中找不到特殊性,将照样照相,把部件放在更换的前线和背面,并打印?

Benchmark gives 0 wallclock secs with your input 

#!/usr/bin/perl
use strict;
use warnings;
use Benchmark;

my $original =  aaaabcd ;
my $replace =  bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb  x 1000;
my $start_time = new Benchmark;
$original =~ s/b/$replace/;
my $end_time = new Benchmark;
my $diff = timediff($end_time,$start_time);
print "Regex took:",timestr($diff);

产出

Regex took 0 wallclock secs (0.00 usr + 0.00 sys = 0.00 CPU)




相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签