Skip to content

Instantly share code, notes, and snippets.

@guitarrapc
Last active February 8, 2017 21:52
Show Gist options
  • 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;
@guitarrapc
Copy link
Author

Function Result

Adding list 0. Elapsed time 10.4587ms
Adding list 1. Elapsed time 37.9896ms
Adding list 2. Elapsed time 79.5513ms
Adding list 3. Elapsed time 94.0689ms
Adding list 4. Elapsed time 96.9513ms
Adding list 5. Elapsed time 97.2735ms
Adding list 6. Elapsed time 97.6232ms
Adding list 7. Elapsed time 97.9385ms
Adding list 8. Elapsed time 98.3093ms
Adding list 9. Elapsed time 98.6885ms
Final elapsed time 99.0148ms
0
1
2
3
4
5
6
7
8
9

@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