2017-11-11 11 views
5

gsubgrep状のベースRの文字列関数を使用する場合は、へのマイナス面があり、習慣の問題として、常にperl = TRUEを指定します?これには欠点がありますか? perl=TRUE正規表現:デフォルトの対「のperl = TRUE」(TRE対PCRE)

、表現がより多くの事を(例えば、あなたが先に見えるか、アサーションの後ろに見て使用することができ、またはあなたが\\Uと小文字の変換を行うことができます)を行うことができ、およびパフォーマンスは、ドキュメントの状態として、同様に高速です。

だから、欠点はありますか? perl = TRUEは下位互換性のためのデフォルトではありませんか? perl = TRUEのときに私が気づくべき移植性に関する懸念はありますか? PCRE正規表現は、制御動詞をバックトラック、前後参照をサポートしていませんTRE正規表現よりもはるかに多くを行うことができますように、オレンジにリンゴを比較するのは良いアイデアではありません

+0

※ご希望の方は、ご利用ください。 –

+1

これらの関数は正規表現を使用します。私は専門家ではありませんが、これらの正規表現のためのいくつかの異なる「味」のAKAエンジンがあることはわかっています。引数がする唯一のことは、perl風味を有効にすることです。 –

+0

'perl = TRUE'は、パターンがPCRE(Perlではない)正規表現エンジンによって処理されるようにします。デフォルトのTRE正規表現エンジンで解析されたパターンとは異なるパターンが動作することに注意してください。 –

答えて

2

予備的検討事項

、ケース・変更オペレーターの置換パターンで(実際には、Rで使用されるPCREライブラリの拡張です)。また、TRE正規表現パターンで.を使用する場合は、改行にも一致し、PCREパターンでは.に一致する改行があることを覚えておいてください。.の前に(?s)インラインDOTALL修飾子を使用する必要があります。 (または修飾子グループのような(?s:.*))。

RでTREとPCREの正規表現エンジンのパフォーマンスを比較する場合は、これらの2つのエンジンと文字通り同じテキストに一致する単純なパターンを使用する必要があります。 Windows 7の

ベンチマークテスト、LinuxのUbuntuの16.04、MacOSのシエラ10.12.6

私は主にWindowsでRを使用しますが、私は特にこのテストのためのLinux VM上でR 3.2.3をインストールしました。 MacOSの結果はt.kalinowski's answerから借りています。

のは、TRE(デフォルト)を比較してみましょうとマイクロベンチマークライブラリを使用してPCRE(perl=TRUE)正規表現のパフォーマンス(benchmarking options in R以上を参照してください):テキストがWikipedia article about butterfliesある

library(microbenchmark) 

WINDOWS 
------- 
Unit: microseconds 
     expr  min  lq  mean median  uq  max neval 
PCRE_1(txt) 163.607 165.418 168.65393 166.625 167.229 7314.588 5e+05 
    TRE_1(txt) 70.031 72.446 74.53842 73.050 74.257 38026.680 5e+05 

MacOS 
----- 
Unit: microseconds 
     expr min  lq  mean median  uq  max neval 
    PCRE_1(txt) 31.693 32.857 37.00757 33.413 35.805 43810.177 5e+05 
    TRE_1(txt) 46.037 47.199 53.06407 47.807 51.981 7702.869 5e+05 

Linux 
------ 
Unit: microseconds 
     expr min  lq  mean median  uq  max neval 
PCRE_1(txt) 10.557 11.555 13.78216 12.097 12.662 4301.178 5e+05 
    TRE_1(txt) 25.875 27.350 31.51925 27.805 28.737 17974.716 5e+05 

TRE正規表現sub

# sub('.*\\((.*)\\).*', '\\1', txt) 
# => [1] "formerly the superfamily \"Hedyloidea\"" 
PCRE_1 <- function(text) { return(sub('.*\\((.*)\\).*', '\\1', txt, perl=TRUE)) } 
TRE_1 <- function(text) { return(sub('.*\\((.*)\\).*', '\\1', txt)) } 
test <- microbenchmark(PCRE_1(txt), TRE_1(txt), times = 500000) 
test 

結果は以下の通りである。

txt <- "Butterflies are insects in the macrolepidopteran clade Rhopalocera from the order Lepidoptera, which also includes moths. Adult butterflies have large, often brightly coloured wings, and conspicuous, fluttering flight. The group comprises the large superfamily Papilionoidea, which contains at least one former group, the skippers (formerly the superfamily \"Hesperioidea\") and the most recent analyses suggest it also contains the moth-butterflies (formerly the superfamily \"Hedyloidea\"). Butterfly fossils date to the Paleocene, which was about 56 million years ago." 

のがしようとするとsub、Rでは非常に一般的sub操作で括弧内の最後のテキストを抽出してみましょうWindowsの場合にのみが勝ち、2倍以上速くなります。 MacOSとLinuxの両方で、PCRE(perl=TRUE)のバージョンは同様の比率で勝ちます。

それでは、重くや二重引用符内の単語を抽出バックトラッキングを使用していない正規表現のパフォーマンスを比較してみましょう:

# regmatches(txt, gregexpr("\"[A-Za-z]+\"", txt)) 
# => [1] "\"Hesperioidea\"" "\"Hedyloidea\"" 
PCRE_2 <- function(text) { return(regmatches(txt, gregexpr("\"[A-Za-z]+\"", txt, perl=TRUE))) } 
TRE_2 <- function(text) { return(regmatches(txt, gregexpr("\"[A-Za-z]+\"", txt))) } 
test <- microbenchmark(PCRE_2(txt), TRE_2(txt), times = 500000) 
test 

WINDOWS 
------- 
Unit: microseconds 
     expr  min  lq  mean median  uq  max neval 
PCRE_2(txt) 324.799 330.232 349.0281 332.646 336.269 124404.14 5e+05 
    TRE_2(txt) 187.755 191.981 204.7663 193.792 196.208 74554.94 5e+05 

MacOS 
----- 
Unit: microseconds 
     expr min  lq  mean median  uq  max neval 
    PCRE_2(txt) 63.801 68.115 75.51773 69.164 71.219 47686.40 5e+05 
    TRE_2(txt) 63.825 67.849 75.20246 68.883 70.933 49691.92 5e+05 

LINUX 
----- 
Unit: microseconds 
     expr min  lq  mean median  uq  max neval 
PCRE_2(txt) 30.199 34.750 44.05169 36.151 43.403 38428.2 5e+05 
    TRE_2(txt) 37.752 41.854 52.58230 43.409 51.781 38915.7 5e+05 

最高の平均値がMacOSの中で、LinuxではPCRE正規表現に属し、その違いはほとんど無視され、Windowsでは、TREがはるかに高速に動作します。

概要

TRE(デフォルト)正規表現ライブラリは、Windowsはるかに高速に動作することは明らかです。 のLinuxでは、PCRE正規表現はかなり高速です。 MacOSでは、バックトラックパターンでPCRE正規表現がそのOSのTREよりも速いので、PCRE正規表現は依然として好ましいです。

+0

偉大な書き込みとベンチマークをありがとう。あなたは['' grep'](https://stat.ethz.ch/R-manual/R-devel/library/base/html/grep.html)に「一般にPCREはデフォルトの正規表現エンジン " –

+1

私のシステムで(macOS 10.12)私は反対の結果(私はこのコメントボックスで動作するようにフォーマットを得ることができないので、答えとして投稿) –

+0

私はtkと同意します実際、ほとんどの場合、perlは固定されたものより速いです。私は一般的にperlが高速だと思うが、いくつかのプラットフォームや実装ではそうではないかもしれない。 – Hugh

0

ベンチマークを@ wiktor-stribiżewで実行すると、私は彼とは異なる結果になります。最初のテストでは、PCREエンジンはTREより高速です(つまり、perl=TRUEが高速です)。 2番目のベンチマークでは、PCREまたはTREの間にパフォーマンスに意味のある違いはありません。 Perlは高速です -

これらはi7-2675QMのCPUの@の2.20GHzは、私の結果のUbuntu 16.04、

``` 
txt <- "Butterflies are insects in the macrolepidopteran clade Rhopalocera from the order Lepidoptera, which also includes moths. Adult butterflies have large, often brightly coloured wings, and conspicuous, fluttering flight. The group comprises the large superfamily Papilionoidea, which contains at least one former group, the skippers (formerly the superfamily \"Hesperioidea\") and the most recent analyses suggest it also contains the moth-butterflies (formerly the superfamily \"Hedyloidea\"). Butterfly fossils date to the Paleocene, which was about 56 million years ago." 

library(microbenchmark) 

PCRE_1 <- function(text) sub('.*\\((.*)\\).*', '\\1', txt, perl=TRUE) 
TRE_1 <- function(text) sub('.*\\((.*)\\).*', '\\1', txt) 
(test <- microbenchmark(PCRE_1(txt), TRE_1(txt), times = 500000)) 
#> Unit: microseconds 
#>   expr min  lq  mean median  uq  max neval 
#> PCRE_1(txt) 31.693 32.857 37.00757 33.413 35.805 43810.177 5e+05 
#> TRE_1(txt) 46.037 47.199 53.06407 47.807 51.981 7702.869 5e+05 

PCRE_2 <- function(text) regmatches(txt, gregexpr("\"[A-Za-z]+\"", txt, perl=TRUE)) 
TRE_2 <- function(text) regmatches(txt, gregexpr("\"[A-Za-z]+\"", txt)) 
(test <- microbenchmark(PCRE_2(txt), TRE_2(txt), times = 500000)) 
#> Unit: microseconds 
#>   expr min  lq  mean median  uq  max neval 
#> PCRE_2(txt) 63.801 68.115 75.51773 69.164 71.219 47686.40 5e+05 
#> TRE_2(txt) 63.825 67.849 75.20246 68.883 70.933 49691.92 5e+05 
``` 
0

、Rバージョン3.4.2(2017年9月28日)、MacOSのシエラ10.12.6上で実行されました、 下記参照。

Unit: microseconds 
     expr min  lq mean median uq max neval cld 
PCRE_1(txt) 8.949 9.809 11.16 10.18 10.62 135299 5e+05 a 
    TRE_1(txt) 23.816 24.805 26.84 25.23 26.17 5433 5e+05 b 

Unit: microseconds 
     expr min lq mean median uq max neval cld 
PCRE_2(txt) 26.97 30.96 37.32 32.19 35.06 243164 5e+05 a 
    TRE_2(txt) 33.75 38.07 44.50 39.40 43.33 35632 5e+05 b 


Session info ----------------------------------------------------------------- 
setting value      
version R version 3.4.2 (2017-09-28) 
system x86_64, linux-gnu   
ui  RStudio (1.1.383)   
language en       
collate en_US.UTF-8     
tz  Europe/Berlin    
date  2017-11-12  



    Linux 4.4.0-93-generiC#116-Ubuntu SMP Fri Aug 11 21:17:51 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux 

model name : Intel(R) Core(TM) i7-4770K CPU @ 3.50GHz 
stepping : 3 
microcode : 0x9 
cpu MHz  : 3647.929 
cache size : 8192 KB