Last active
February 8, 2017 21:52
-
-
Save guitarrapc/e092ffb40e06fec40216 to your computer and use it in GitHub Desktop.
PowerShell 5.0 using namespace syntax for .NET Operation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Require -Version 5.0 | |
# PowerShell 5.0 now supports <using namespace NameSpace> sysntax like C#! | |
using namespace System; | |
using namespace System.Text; | |
using namespace System.Diagnostics; | |
using namespace System.Linq; | |
using namespace System.Collections.Generic; | |
class NameSpaceSyntaxTest | |
{ | |
# you can not use using namespace for Class output type. | |
# [List[int]] Main() # unable to find type List[int] | |
static [System.Collections.Generic.List[int]] Main() | |
{ | |
# Stopswatch | |
$sw = [Stopwatch]::StartNew(); | |
# List<int> | |
$oldStyleListDeclare = New-Object "System.Collections.Generic.List[int]"; | |
$newStyleListDeclare = New-Object List[int]; | |
# Add item to List<int> | |
[Enumerable]::Range(0,10) ` | |
| % { | |
[Console]::WriteLine("Adding list $_. Elapsed time $($sw.Elapsed.TotalMilliseconds)ms"); | |
$newStyleListDeclare.Add($_); | |
}; | |
# show final message | |
$sw.Stop(); | |
[Console]::WriteLine("Final elapsed time $($sw.Elapsed.TotalMilliseconds)ms"); | |
# Return List result | |
return $newStyleListDeclare; | |
} | |
} | |
[NameSpaceSyntaxTest]::Main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Require -Version 5.0 | |
# PowerShell 5.0 now supports <using namespace NameSpace> sysntax like C#! | |
using namespace System; | |
using namespace System.Text; | |
using namespace System.Diagnostics; | |
using namespace System.Linq; | |
using namespace System.Collections.Generic; | |
function Main | |
{ | |
[OutputType([System.Collections.Generic.List[int]])] | |
[CmdletBinding()] | |
param() | |
# Stopswatch | |
$sw = [Stopwatch]::StartNew(); | |
# List<int> | |
$oldStyleListDeclare = New-Object "System.Collections.Generic.List[int]"; | |
$newStyleListDeclare = New-Object List[int]; | |
# Add item to List<int> | |
[Enumerable]::Range(0,10) ` | |
| % { | |
[Console]::WriteLine("Adding list $_. Elapsed time $($sw.Elapsed.TotalMilliseconds)ms"); | |
$newStyleListDeclare.Add($_); | |
}; | |
# show final message | |
$sw.Stop(); | |
[Console]::WriteLine("Final elapsed time $($sw.Elapsed.TotalMilliseconds)ms"); | |
# show List result | |
return $newStyleListDeclare; | |
} | |
Main; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
FWIW 99% of time you measure is spent in internal PowerShell stuff which is not affected by class/function difference in optimization: Linq.Range, pipeline intrinsics, scriptbock context creation, console function invocation, console output itself.
The code below is 100+ times faster as compared to the original one on i7 CPU: