Skip to content

Instantly share code, notes, and snippets.

@guitarrapc
Last active February 8, 2017 21:52
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guitarrapc/e092ffb40e06fec40216 to your computer and use it in GitHub Desktop.
Save guitarrapc/e092ffb40e06fec40216 to your computer and use it in GitHub Desktop.
PowerShell 5.0 using namespace syntax for .NET Operation
#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();
#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;
@tophf
Copy link

tophf commented Feb 4, 2017

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:

$time = foreach ($_ in 0..9) {
    "Adding list $_. Elapsed time $($sw.Elapsed.TotalMilliseconds)ms"
    $newStyleListDeclare.Add($_) >$null
}
$sw.Stop()
[Console]::WriteLine($time -join "`n")

Adding list 0. Elapsed time 0.0178ms
Adding list 1. Elapsed time 0.0281ms
Adding list 2. Elapsed time 0.0307ms
Adding list 3. Elapsed time 0.0331ms
Adding list 4. Elapsed time 0.0351ms
Adding list 5. Elapsed time 0.0375ms
Adding list 6. Elapsed time 0.0395ms
Adding list 7. Elapsed time 0.0416ms
Adding list 8. Elapsed time 0.0436ms
Adding list 9. Elapsed time 0.0466ms
Final elapsed time 0.0524ms

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment