PowerShell -match vs -like

前端 未结 3 1210
萌比男神i
萌比男神i 2020-12-21 08:41

Reading official docs it\'s obvious that PowerShell -match operator is more powerful than -like (due to regular expressions). Secondly, it seems ~1

3条回答
  •  半阙折子戏
    2020-12-21 09:20

    I've never seen -match test that much faster than -like, if at all. Normally I see -like at about the same or better speed.

    But I never rely on one test instance, I usually run through about 10K reps of each.

    If your're looking for performance, always prefer string methods if they'll meet the requirements:

    $string = '123abc'    
    
    (measure-command {
    for ($i=0;$i -lt 1e5;$i++)
     {$string.contains('3ab')}
    }).totalmilliseconds
    
    (measure-command {
    for ($i=0;$i -lt 1e5;$i++)
     {$string -like '*3ab*'}
    }).totalmilliseconds
    
    (measure-command {
    for ($i=0;$i -lt 1e5;$i++)
     {$string -match '3ab'}
    }).totalmilliseconds
    
    265.3494
    586.424
    646.4878
    

提交回复
热议问题