Author Posts

January 30, 2017 at 5:48 am

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

January 30, 2017 at 7:29 am

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.

January 30, 2017 at 7:34 am

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

January 30, 2017 at 9:45 am

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.

January 30, 2017 at 9:51 am

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

January 30, 2017 at 10:02 am

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

January 30, 2017 at 11:33 am

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.

January 30, 2017 at 11:40 am

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