Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

commented Aug 29, 2015

Class Result

Adding list 0. Elapsed time 7.3973ms
Adding list 1. Elapsed time 14.068ms
Adding list 2. Elapsed time 14.5463ms
Adding list 3. Elapsed time 14.9423ms
Adding list 4. Elapsed time 15.5296ms
Adding list 5. Elapsed time 15.8406ms
Adding list 6. Elapsed time 16.1545ms
Adding list 7. Elapsed time 16.4804ms
Adding list 8. Elapsed time 16.8308ms
Adding list 9. Elapsed time 17.1498ms
Final elapsed time 19.3839ms
0
1
2
3
4
5
6
7
8
9
@guitarrapc

This comment has been minimized.

Copy link
Owner Author

commented Aug 29, 2015

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

This comment has been minimized.

Copy link

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
You can’t perform that action at this time.