Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell -match vs -like

Reading official docs it's obvious that PowerShell -match operator is more powerful than -like (due to regular expressions). Secondly, it seems ~10 times faster according to this article https://www.pluralsight.com/blog/software-development/powershell-operators-like-match.

Are there specific cases when I should prefer -like instead of -match? If there not, why at all should I use -like? Does it exist because of historical reasons?

like image 667
Bad Avatar asked Dec 06 '22 18:12

Bad


1 Answers

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
like image 131
mjolinor Avatar answered Dec 14 '22 02:12

mjolinor