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?
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.
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.
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.
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.