# Measure-Commands (match or contains)

**URL:** <https://forums.powershell.org/t/measure-commands-match-or-contains/7947>\
**Category:** PowerShell Help\
**Created:** [January 30, 2017, 12:18am UTC](https://forums.powershell.org/t/measure-commands-match-or-contains/7947 "2017-01-30T00:18:07Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![blackboxcoder](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/blackboxcoder/32/5314_2.png) [@blackboxcoder](https://forums.powershell.org/u/blackboxcoder)\
**Post date:** [January 30, 2017, 12:18am UTC](https://forums.powershell.org/t/measure-commands-match-or-contains/7947/1 "2017-01-30T00:18:07Z")

</div>

Hey Folks,

I stuck a bit with measuring. After watching a video, how to build advanced PowerShell-Functions I wanted to know, what is faster: $String -match ‘^C:\’ or $String -contains ‘C:\*’. After I measured this, the solution is, that -match is 1ms faster… but when I do for example a forloop in it, the -contains method is faster about 27ms. I have no glue why, any ideas?

```
$path = "C:\Windows"

Measure-Command{
    if($path -match '^C:\\'){
        for($X=1; $X -lt 500; $X++){
            Write-Output "Contains"
        }
    }
}#faster 1x

Measure-Command{
    if($path -contains "C:\"){
        for($X=1; $X -lt 500; $X++){
            Write-Output "Contains"
        }
    }
}#faster 500x
```

BR,  
BBC

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [January 30, 2017, 1:59am UTC](https://forums.powershell.org/t/measure-commands-match-or-contains/7947/2 "2017-01-30T01:59:21Z")

</div>

Actually it does not make sense to compare them because they do different things. “-Contains” checks if a specific item is in a collection and “-match” tries to find a pattern in a string.

---

<div class="post-metadata">

**Author:** ![blackboxcoder](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/blackboxcoder/32/5314_2.png) [@blackboxcoder](https://forums.powershell.org/u/blackboxcoder)\
**Post date:** [January 30, 2017, 2:04am UTC](https://forums.powershell.org/t/measure-commands-match-or-contains/7947/3 "2017-01-30T02:04:54Z")

</div>

Ohh okay, I didn’t see that!  
Thank you. I will try it with -like 🙂

Edit:

```
$path = "C:\Windows"

Measure-Command{
    if($path -match '^C:\\'){
        for($X=1; $X -lt 500; $X++){
            Write-Output "Contains"
        }
    }
}#faster 1x

Measure-Command{
    if($path -like "C:\*"){
        for($X=1; $X -lt 500; $X++){
            Write-Output "Contains"
        }
    }
}#faster 500x
```

Okay, like is faster about 2ms (1 times).  
Now is the question, why?

BR,  
BBC

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [January 30, 2017, 4:15am UTC](https://forums.powershell.org/t/measure-commands-match-or-contains/7947/4 "2017-01-30T04:15:07Z")

</div>

Because they don’t do the same under the hood. Even if it seems to produce similar results.

Those kind of comparisons are useless if you don’t have a particular use case. Of course some commands run faster than others because they do different things.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [January 30, 2017, 4:21am UTC](https://forums.powershell.org/t/measure-commands-match-or-contains/7947/5 "2017-01-30T04:21:09Z")

</div>

BTW: If you’re interested - there is a German Powershell Forum. Sometimes it is easier to explain or even to ask in your native/first language. 😉

[https://social.technet.microsoft.com/Forums/de-de/home?forum=powershell\_de](https://social.technet.microsoft.com/Forums/de-de/home?forum=powershell_de)

---

<div class="post-metadata">

**Author:** ![blackboxcoder](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/blackboxcoder/32/5314_2.png) [@blackboxcoder](https://forums.powershell.org/u/blackboxcoder)\
**Post date:** [January 30, 2017, 4:32am UTC](https://forums.powershell.org/t/measure-commands-match-or-contains/7947/6 "2017-01-30T04:32:39Z")

</div>

Thank you a lot for the explanation.  
I try to improve my skills, with PowerShell and also in English writing.

Is there a way to have a look under the hood for a method or a command?  
Because I want to optimize the code, by knowing what command/methods will perform in the background and not measure everything.

How do you detected where I am from?

BR,  
BBC

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [January 30, 2017, 6:03am UTC](https://forums.powershell.org/t/measure-commands-match-or-contains/7947/7 "2017-01-30T06:03:55Z")

</div>

> I try to improve my skills, with PowerShell and also in English writing.

Great. Both thumbs up. ;-)

> Is there a way to have a look under the hood for a method or a command?

Hmmm ... yes and no. As far as I know there is no easy way but you could do reverse engeneering if you really want to.

> Because I want to optimize the code, by knowing what command/methods will perform in the background and not measure everything.

Powershell improved a lot over the last years, but it is still scripting technology. Most of the time performance does not matter that much. If it matters - measure it. ;-) Pretty often you can use .net code to speed up some tasks but I think most of the time it is enough to go with consistent Powershell cmdlets. That's more reliable and maintainable.

> How do you detected where I am from?

You told me. ;-) You posted a link to your blog in your profile.

---

<div class="post-metadata">

**Author:** ![blackboxcoder](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/blackboxcoder/32/5314_2.png) [@blackboxcoder](https://forums.powershell.org/u/blackboxcoder)\
**Post date:** [January 30, 2017, 6:10am UTC](https://forums.powershell.org/t/measure-commands-match-or-contains/7947/8 "2017-01-30T06:10:40Z")

</div>

ohh, sometimes I am a little bit blind. yeah.

Okay, earlier before I just wrote C# code.  
So I started with PowerShell mid/end of the last year.

But I practice each day, with administrativ scenarios. I saved some money yet 🙂

I think with the time-spent-balance in mind it is better to reach the perfect solution with measuring instead of reverse engineering every command.

Cheers and thank you 😃

Have a nice day.

BR,  
BBC

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:39pm UTC](https://forums.powershell.org/t/measure-commands-match-or-contains/7947/9 "2024-05-16T20:39:34Z")

</div>

Closed on May 16, 2024, 8:39pm UTC


