tech.guitarrapc.cóm

Technical updates

PowerShellでSubstitute(変数代入)、Host(ホスト出力)、Content(ファイル出力)するコストのベンチマーク

どうも、ベンチマーク厨です。 いや、そうではないのですが、コマンドレットはAutomationしているわけで中々内側が見えません。 実際、同じような利用ができるコマンドレットは多く、スピード的な一面も押さえておいて損はないと思います。 そこで、今回は、前回の前提を基に引き続きベンチマークを取ってみます。

日本語記事: PowerShellでFor / Foreach / Foreach-Object / ScriptBlock / Filterのベンチマーク
English Article: PowerShell For / Foreach / Foreach-Object / ScriptBlock / Filter – Benchmark 2013

はじめに

今回のお題目は…これらのコストはいかほどかです。

変数への代入 (Substitute/Assign to the variable) ホスト画面出力 (Host Output) ファイル出力 (File Output)

本来ならPowerShell 1.0, 2.0の結果も併記するべきなのでしょうが、PowerShell 3.0のみです。 どうかご了承下さい。

測定対象

前回同様、forforeach以外にも多用される、Foreach-Objectコマンドレット、ScriptBlockfilterを対象に測定しています。

for
foreach
Foreach-Object
ScriptBlock
Filter

いわずとしてれますが、PowerShellで利用する際はこのような感じです。

for (<初期化パイプライン>; <評価パイプライン>;<インクリメントパイプライン>){<文リスト>}
foreach (<変数> in <ループにかけるパイプライン>){<文リスト>}
<パラメータ> | Foreach-Object {begin}{process}{end}
&{文リスト}
filter <名前>{param(<パラメーターリスト>) <文リスト>}

そして今回は各ループ処理に以下のソースを追加してコストが変わるかでベンチを取ります。

$hoge = $total # 変数への代入
$Global:hoge = $total # グローバル変数への代入

Write-Host $total # ホスト画面出力
[console]::WriteLine("$total") # .Netを利用ホスト画面出力

$total | Out-File # ファイル出力
Add-Content -Value $total # ファイル出力

ベンチマーク結果

前回の記事で、コレクション長によって指数関数的にコストが増えるわけではないことが分かっています。 そこで、コレクション長は10000とします。

計測単位 : miliseconds コレクション長 : 10000

変数代入

  1. None : 前回同様$totalにコレクションを足す以外、何もしていないベースの状態です。
  2. Substitute : Noneに加えて、子スコープのままで変数に代入
  3. Substitute_Global : Noneに加えて、グローバル変数を呼び出し子スコープ変数を代入
Data Type Collection Type Loop Type None Substitute Substitute_Global
int int For 137.41 170.92 169.46
Foreach 91.28 116.00 115.36
Foreach-Object 580.89 643.32 648.66
ScriptBlock 113.65 129.87 144.46
Filter 115.12 133.95 147.32
List<int> For 201.78 225.60 232.92
Foreach 144.20 163.92 174.82
Foreach-Object 636.81 700.66 736.98
ScriptBlock 174.52 195.21 202.49
Filter 174.17 182.78 208.25
ArrayList<int> For 144.24 162.28 171.57
Foreach 88.62 107.99 114.61
Foreach-Object 561.38 599.04 619.76
ScriptBlock 111.93 126.38 139.84
Filter 115.02 122.40 139.32
Average 226.07 252.02 264.39

ホスト出力

  1. None : 前回同様$totalにコレクションを足す以外、何もしていないベースの状態です。
  2. Write-Output(Object) : Write-OutputコマンドレットでObject出力された場合 (本コマンドレットは状況に応じて自動的にObject出力とHost出力が切り替わります)
  3. Write-Output() : Write-Outputコマンドレットでホスト画面に出力
  4. Write-Host : Write-Hostコマンドレットでホスト画面に出力
  5. Console::Write : [System.Console]::WriteLineメソッドでホスト画面に出力
Data Type Collection Type Loop Type None Write-Output(Object) Write-Output(Host) Write-Host Console::Write
int int For 137.41 1587.95 6380.74 6352.40 2261.77
Foreach 91.28 1513.34 6563.93 5844.37 2120.81
Foreach-Object 580.89 2204.04 7007.28 7469.18 2915.34
ScriptBlock 113.65 1623.87 6133.83 6183.97 2196.14
Filter 115.12 1620.46 6195.55 6071.62 2275.90
List<int> For 201.78 1710.05 6380.76 6166.34 2364.22
Foreach 144.20 1623.27 6396.59 6123.73 2261.01
Foreach-Object 636.81 2278.93 7092.62 7269.10 3043.07
ScriptBlock 174.52 1744.01 6552.76 6512.89 2360.54
Filter 174.17 1750.13 6462.41 6407.75 2324.75
ArrayList<int> For 144.24 1588.09 6281.62 5829.04 2217.66
Foreach 88.62 1511.92 6022.70 5700.31 2088.57
Foreach-Object 561.38 2173.55 6742.15 6741.88 2896.07
ScriptBlock 111.93 1603.57 6052.65 5969.21 2155.02
Filter 115.02 1607.19 5956.31 5797.68 2198.74
Average 226.07 1742.69 6414.79 6295.97 2378.64

ファイル出力

  1. None : 前回同様$totalにコレクションを足す以外、何もしていないベースの状態です。
  2. SetContent: AddContentコマンドレットでファイルに追記出力
  3. OutFile : Out-Fileコマンドレットでファイルに追記出力
Data Type Collection Type Loop Type None SetContent OutFile
int int For 137.41 9109.02 9126.99
Foreach 91.28 9005.38 9016.87
Foreach-Object 580.89 9476.49 9822.08
ScriptBlock 113.65 8977.33 9128.05
Filter 115.12 9147.63 9187.12
List<int> For 201.78 9078.84 9272.52
Foreach 144.20 9188.92 8990.75
Foreach-Object 636.81 9463.82 9673.35
ScriptBlock 174.52 9198.39 9159.49
Filter 174.17 9143.69 9151.33
ArrayList<int> For 144.24 8979.03 8704.22
Foreach 88.62 9109.20 8613.43
Foreach-Object 561.38 9534.32 9202.77
ScriptBlock 111.93 9173.08 8715.47
Filter 115.02 9194.50 8880.19
Average 226.07 9185.31 9109.64

結果

それでは、全体の結果を見てみます。

コレクション別速度順位

いずれも、コレクションを直接処理した前回に比べるとループ処理によるコストの差は大幅に縮んでいます。 これはもう、ほど無視できるレベルと言えるかと思います。 foreach v.s. Foreach-Objectで見てみましょう。

  1. 変数代入では、、3~4倍程度です。
  2. ホスト出力では、1.2倍~1.4倍程度です。
  3. ファイル出力では、1.05倍程度です。
  1. ArrayList (Fastest)
  2. int
  3. List (Slowest)

Data Type別 Loop操作

これもループタイプと同様に、コレクションを直接処理した前回に比べるとデータタイプによるコストの差は大幅に縮んでいます。 intとArrayListでも、もはや1割程度の違いです。

  • int (Array)
  • List
  • ArrayList

Racioから特筆すべきは、Foreach-Objectが、コマンド毎のコストの違いに差が小さい事です。 元々のコマンドのコストが高いこともありますが、Foreach-Objectの特徴である利用メモリの小ささが要因にありそうですね。

各操作のコスト

代入、ホスト出力、ファイル出力のそれぞれをみてみましょう。

代入 - Substitute/Assign

代入のコストは、何もしない場合(None)の1~2割程度と非常に小さいですね。 子スコープでの代入、グローバル変数への代入はほぼ変わりませんでした。

ホスト出力 - Host Output

ホスト出力は、何もしない場合(None)に比べ大幅にコストが高まることが分かります。 対Write-Output(Object)は、ホスト画面出力が無い分5-10倍前後です。 対Write-Output(Host)は、ホスト画面出力をしており、25-30倍前後です。 対Write-Hostで、なんと25~30倍前後です。 対[System.Console]::WriteLine()で、5~10倍前後です。 また、Write-Outputに比べて、Write-Hostのコストがわずかですが低いようですね。 特に[System.Console]::WriteLine()は、Write-Hostに比べて1/3程度のコストを示す場合もあり、非常に早いことが分かります。

ファイル出力 - File Output

ファイル出力は、何もしない場合(None)に比べホスト出力以上にコストがかかることが分かります。 また、Add-Content(Set-Content)でも、Out-Fileでもコストはほぼ同一です。 Noneに対して、70倍前後です。 また、他のコマンドと異なり、DataTypeやLoop Typeにかかわらずほぼ一定のコストのようにも見えます。 ※多少、Foreach-Objectが遅いようですが。 これは、Out-Fileがパイプを介していることを考えるとパイプのコストは意外と小さい可能性がありますね。

テストに利用したコード

何もしていない基準となるNoneのコードです。

filter Get-FilterTest{

        [decimal]$total += $_
}

function Get-intForTest{

    param(
    $int
    )

    Measure-Command{
        for ($i = 0; $i -lt $int.length; $i++)
        {
            [decimal]$total += $int[$i]
        }
    }

}

function Get-intForeachTest{

    param(
    $int
    )

    Measure-Command{
        foreach ($i in $int)
        {
            [decimal]$total += $i
        }
    }

}

function Get-intForeachObjectTest{

    param(
    $int
    )

    Measure-Command{
        $int | ForEach-Object{
            [decimal]$total += $_
        }
    }

}

function Get-intScriptBlockTest{

    param(
    $int
    )

    Measure-Command{
        $int | &{process{[decimal]$total += $_}}
    }

}

function Get-intFilterTest{

    param(
    $int
    )

    Measure-Command{
        $int | Get-FilterTest
    }
}

function Get-ListForTest{

	param(
	$int
	)

	$list  = New-Object 'System.Collections.Generic.List`1[System.String]'

	$int | foreach-Object { $list.Add($_)}

	#,$list

    Measure-Command{
	    for ($i = 0; $i -lt $int.length; $i++)
        {
    	    [decimal]$total += $list[$i]
        }
    }
}

function Get-ListForeachTest{

	param(
	$int
	)

	$list  = New-Object 'System.Collections.Generic.List`1[System.String]'

	$int | foreach-Object { $list.Add($_)}

	#,$list

    Measure-Command{
	    foreach ($l in $list)
        {
    	    [decimal]$total += $l
        }
    }
}

function Get-ListForeachObjectTest{

	param(
	$int
	)

	$list  = New-Object 'System.Collections.Generic.List`1[System.String]'

	$int | foreach-Object { $list.Add($_)}

	#,$list

    Measure-Command{
	    $list | foreach-Object{
    	    [decimal]$total += $_
        }
    }
}

function Get-ListScriptBlockTest{

	param(
	$int
	)

	$list  = New-Object 'System.Collections.Generic.List`1[System.String]'

	$int | foreach-Object { $list.Add($_)}

	#,$list

    Measure-Command{
	    $list | &{process{[decimal]$total += $_}}
    }
}

function Get-ListFilterTest{
    param(
    $int
    )

	$list  = New-Object 'System.Collections.Generic.List`1[System.String]'

	$int | foreach-Object { $list.Add($_)}

	#,$list

    Measure-Command{
        $list | Get-FilterTest
    }
}

function Get-ArrayListForTest{
    param(
    $int
    )

	$arrayList = New-Object System.Collections.ArrayList

	[Void]($int | foreach-Object { $arrayList.Add($_) })

	#,$Arraylist

    Measure-Command{
	    for ($i = 0; $i -lt $int.length; $i++)
        {
    	    [decimal]$total += $arrayList[$i]
        }
    }
}

function Get-ArrayListForeachTest{
    param(
    $int
    )

	$arrayList = New-Object System.Collections.ArrayList

	[Void]($int | foreach-Object { $arrayList.Add($_) })

	#,$Arraylist

    Measure-Command{
	    foreach ($al in $arrayList)
        {
    	    [decimal]$total += $al
        }
    }
}

function Get-ArrayListForeachObjectTest{
    param(
    $int
    )

	$arrayList = New-Object System.Collections.ArrayList

	[Void]($int | foreach-Object { $arrayList.Add($_) })

	#,$Arraylist

    Measure-Command{
	    $arrayList | Foreach-Object{
    	    [decimal]$total += $_
        }
    }
}

function Get-ArrayListScriptBlockTest{
    param(
    $int
    )

	$arrayList = New-Object System.Collections.ArrayList

	[Void]($int | foreach-Object { $arrayList.Add($_) })

	#,$Arraylist

    Measure-Command{
	    $arrayList | &{process{[decimal]$total += $_}}
    }
}

filter Get-ArrayListFilterTest{
    param(
    $int
    )

	$arrayList = New-Object System.Collections.ArrayList

	[Void]($int | foreach-Object { $arrayList.Add($_) })

	#,$Arraylist
	Measure-Command {
	    $arrayList | Get-FilterTest
    }
}

[int[]]$array=@()
#$array += 1..30000000
$array += 1..10000

1..4 | ForEach-Object {
(Get-intForTest -int $array).TotalMilliseconds
(Get-intForeachTest -int $array).TotalMilliseconds
(Get-intForeachObjectTest -int $array).TotalMilliseconds
(Get-intScriptBlockTest -int $array).TotalMilliseconds
(Get-intFilterTest -int $array).TotalMilliseconds

(Get-ListForTest -int $array).TotalMilliseconds
(Get-ListForeachTest -int $array).TotalMilliseconds
(Get-ListForeachObjectTest -int $array).TotalMilliseconds
(Get-ListScriptBlockTest -int $array).TotalMilliseconds
(Get-ListFilterTest -int $array).TotalMilliseconds

(Get-ArrayListForTest -int $array).TotalMilliseconds
(Get-ArrayListForeachTest -int $array).TotalMilliseconds
(Get-ArrayListForeachObjectTest -int $array).TotalMilliseconds
(Get-ArrayListScriptBlockTest -int $array).TotalMilliseconds
(Get-ArrayListFilterTest -int $array).TotalMilliseconds
""
}

代入

子スコープのまま代入のコードです

filter Get-FilterTest{

        [decimal]$total += $_
        $hoge = $total
}

function Get-intForTest{

    param(
    $int
    )

    Measure-Command{
        for ($i = 0; $i -lt $int.length; $i++)
        {
            [decimal]$total += $int[$i]
            $hoge = $total
        }
    }

}

function Get-intForeachTest{

    param(
    $int
    )

    Measure-Command{
        foreach ($i in $int)
        {
            [decimal]$total += $i
            $hoge = $total
        }
    }

}

function Get-intForeachObjectTest{

    param(
    $int
    )

    Measure-Command{
        $int | ForEach-Object{
            [decimal]$total += $_
            $hoge = $total
        }
    }

}

function Get-intScriptBlockTest{

    param(
    $int
    )

    Measure-Command{
        $int | &{
            process{
                [decimal]$total += $_
                $hoge = $total
            }
        }
    }
}

function Get-intFilterTest{

    param(
    $int
    )

    Measure-Command{
        $int | Get-FilterTest
    }
}

function Get-ListForTest{

	param(
	$int
	)

	$list  = New-Object 'System.Collections.Generic.List`1[System.String]'

	$int | foreach-Object { $list.Add($_)}

	#,$list

    Measure-Command{
	    for ($i = 0; $i -lt $int.length; $i++)
        {
    	    [decimal]$total += $list[$i]
            $hoge = $total
        }
    }
}

function Get-ListForeachTest{

	param(
	$int
	)

	$list  = New-Object 'System.Collections.Generic.List`1[System.String]'

	$int | foreach-Object { $list.Add($_)}

	#,$list

    Measure-Command{
	    foreach ($l in $list)
        {
    	    [decimal]$total += $l
            $hoge = $total
        }
    }
}

function Get-ListForeachObjectTest{

	param(
	$int
	)

	$list  = New-Object 'System.Collections.Generic.List`1[System.String]'

	$int | foreach-Object { $list.Add($_)}

	#,$list

    Measure-Command{
	    $list | foreach-Object{
    	    [decimal]$total += $_
            $hoge = $total
        }
    }
}

function Get-ListScriptBlockTest{

	param(
	$int
	)

	$list  = New-Object 'System.Collections.Generic.List`1[System.String]'

	$int | foreach-Object { $list.Add($_)}

	#,$list

    Measure-Command{
	    $list | &{
            process{
                [decimal]$total += $_
                $hoge = $total
            }
        }
    }
}

function Get-ListFilterTest{
    param(
    $int
    )

	$list  = New-Object 'System.Collections.Generic.List`1[System.String]'

	$int | foreach-Object { $list.Add($_)}

	#,$list

    Measure-Command{
        $list | Get-FilterTest
    }
}

function Get-ArrayListForTest{
    param(
    $int
    )

	$arrayList = New-Object System.Collections.ArrayList

	[Void]($int | foreach-Object { $arrayList.Add($_) })

	#,$Arraylist

    Measure-Command{
	    for ($i = 0; $i -lt $int.length; $i++)
        {
    	    [decimal]$total += $arrayList[$i]
            $hoge = $total
        }
    }
}

function Get-ArrayListForeachTest{
    param(
    $int
    )

	$arrayList = New-Object System.Collections.ArrayList

	[Void]($int | foreach-Object { $arrayList.Add($_) })

	#,$Arraylist

    Measure-Command{
	    foreach ($al in $arrayList)
        {
    	    [decimal]$total += $al
            $hoge = $total
        }
    }
}

function Get-ArrayListForeachObjectTest{
    param(
    $int
    )

	$arrayList = New-Object System.Collections.ArrayList

	[Void]($int | foreach-Object { $arrayList.Add($_) })

	#,$Arraylist

    Measure-Command{
	    $arrayList | Foreach-Object{
    	    [decimal]$total += $_
            $hoge = $total
        }
    }
}

function Get-ArrayListScriptBlockTest{
    param(
    $int
    )

	$arrayList = New-Object System.Collections.ArrayList

	[Void]($int | foreach-Object { $arrayList.Add($_) })

	#,$Arraylist

    Measure-Command{
	    $arrayList | &{
            process{
                [decimal]$total += $_
                $hoge = $total
            }
        }
    }
}

filter Get-ArrayListFilterTest{
    param(
    $int
    )

	$arrayList = New-Object System.Collections.ArrayList

	[Void]($int | foreach-Object { $arrayList.Add($_) })

	#,$Arraylist
	Measure-Command {
	    $arrayList | Get-FilterTest
    }
}

[int[]]$array=@()
#$array += 1..30000000
$array += 1..10000

1..4 | ForEach-Object {
(Get-intForTest -int $array).TotalMilliseconds
(Get-intForeachTest -int $array).TotalMilliseconds
(Get-intForeachObjectTest -int $array).TotalMilliseconds
(Get-intScriptBlockTest -int $array).TotalMilliseconds
(Get-intFilterTest -int $array).TotalMilliseconds

(Get-ListForTest -int $array).TotalMilliseconds
(Get-ListForeachTest -int $array).TotalMilliseconds
(Get-ListForeachObjectTest -int $array).TotalMilliseconds
(Get-ListScriptBlockTest -int $array).TotalMilliseconds
(Get-ListFilterTest -int $array).TotalMilliseconds

(Get-ArrayListForTest -int $array).TotalMilliseconds
(Get-ArrayListForeachTest -int $array).TotalMilliseconds
(Get-ArrayListForeachObjectTest -int $array).TotalMilliseconds
(Get-ArrayListScriptBlockTest -int $array).TotalMilliseconds
(Get-ArrayListFilterTest -int $array).TotalMilliseconds
""
}

親スコープへ代入のコードです

filter Get-FilterTest{

        [decimal]$total += $_
        $Global:hoge = $total
}

function Get-intForTest{

    param(
    $int
    )

    Measure-Command{
        for ($i = 0; $i -lt $int.length; $i++)
        {
            [decimal]$total += $int[$i]
            $Global:hoge = $total
        }
    }

}

function Get-intForeachTest{

    param(
    $int
    )

    Measure-Command{
        foreach ($i in $int)
        {
            [decimal]$total += $i
            $Global:hoge = $total
        }
    }

}

function Get-intForeachObjectTest{

    param(
    $int
    )

    Measure-Command{
        $int | ForEach-Object{
            [decimal]$total += $_
            $Global:hoge = $total
        }
    }

}

function Get-intScriptBlockTest{

    param(
    $int
    )

    Measure-Command{
        $int | &{
            process{
                [decimal]$total += $_
                $Global:hoge = $total
            }
        }
    }
}

function Get-intFilterTest{

    param(
    $int
    )

    Measure-Command{
        $int | Get-FilterTest
    }
}

function Get-ListForTest{

	param(
	$int
	)

	$list  = New-Object 'System.Collections.Generic.List`1[System.String]'

	$int | foreach-Object { $list.Add($_)}

	#,$list

    Measure-Command{
	    for ($i = 0; $i -lt $int.length; $i++)
        {
    	    [decimal]$total += $list[$i]
            $Global:hoge = $total
        }
    }
}

function Get-ListForeachTest{

	param(
	$int
	)

	$list  = New-Object 'System.Collections.Generic.List`1[System.String]'

	$int | foreach-Object { $list.Add($_)}

	#,$list

    Measure-Command{
	    foreach ($l in $list)
        {
    	    [decimal]$total += $l
            $Global:hoge = $total
        }
    }
}

function Get-ListForeachObjectTest{

	param(
	$int
	)

	$list  = New-Object 'System.Collections.Generic.List`1[System.String]'

	$int | foreach-Object { $list.Add($_)}

	#,$list

    Measure-Command{
	    $list | foreach-Object{
    	    [decimal]$total += $_
            $Global:hoge = $total
        }
    }
}

function Get-ListScriptBlockTest{

	param(
	$int
	)

	$list  = New-Object 'System.Collections.Generic.List`1[System.String]'

	$int | foreach-Object { $list.Add($_)}

	#,$list

    Measure-Command{
	    $list | &{
            process{
                [decimal]$total += $_
                $Global:hoge = $total
            }
        }
    }
}

function Get-ListFilterTest{
    param(
    $int
    )

	$list  = New-Object 'System.Collections.Generic.List`1[System.String]'

	$int | foreach-Object { $list.Add($_)}

	#,$list

    Measure-Command{
        $list | Get-FilterTest
    }
}

function Get-ArrayListForTest{
    param(
    $int
    )

	$arrayList = New-Object System.Collections.ArrayList

	[Void]($int | foreach-Object { $arrayList.Add($_) })

	#,$Arraylist

    Measure-Command{
	    for ($i = 0; $i -lt $int.length; $i++)
        {
    	    [decimal]$total += $arrayList[$i]
            $Global:hoge = $total
        }
    }
}

function Get-ArrayListForeachTest{
    param(
    $int
    )

	$arrayList = New-Object System.Collections.ArrayList

	[Void]($int | foreach-Object { $arrayList.Add($_) })

	#,$Arraylist

    Measure-Command{
	    foreach ($al in $arrayList)
        {
    	    [decimal]$total += $al
            $Global:hoge = $total
        }
    }
}

function Get-ArrayListForeachObjectTest{
    param(
    $int
    )

	$arrayList = New-Object System.Collections.ArrayList

	[Void]($int | foreach-Object { $arrayList.Add($_) })

	#,$Arraylist

    Measure-Command{
	    $arrayList | Foreach-Object{
    	    [decimal]$total += $_
            $Global:hoge = $total
        }
    }
}

function Get-ArrayListScriptBlockTest{
    param(
    $int
    )

	$arrayList = New-Object System.Collections.ArrayList

	[Void]($int | foreach-Object { $arrayList.Add($_) })

	#,$Arraylist

    Measure-Command{
	    $arrayList | &{
            process{
                [decimal]$total += $_
                $Global:hoge = $total
            }
        }
    }
}

filter Get-ArrayListFilterTest{
    param(
    $int
    )

	$arrayList = New-Object System.Collections.ArrayList

	[Void]($int | foreach-Object { $arrayList.Add($_) })

	#,$Arraylist
	Measure-Command {
	    $arrayList | Get-FilterTest
    }
}

[int[]]$array=@()
#$array += 1..30000000
$array += 1..10000

$hoge = $null

1..4 | ForEach-Object {
(Get-intForTest -int $array).TotalMilliseconds
(Get-intForeachTest -int $array).TotalMilliseconds
(Get-intForeachObjectTest -int $array).TotalMilliseconds
(Get-intScriptBlockTest -int $array).TotalMilliseconds
(Get-intFilterTest -int $array).TotalMilliseconds

(Get-ListForTest -int $array).TotalMilliseconds
(Get-ListForeachTest -int $array).TotalMilliseconds
(Get-ListForeachObjectTest -int $array).TotalMilliseconds
(Get-ListScriptBlockTest -int $array).TotalMilliseconds
(Get-ListFilterTest -int $array).TotalMilliseconds

(Get-ArrayListForTest -int $array).TotalMilliseconds
(Get-ArrayListForeachTest -int $array).TotalMilliseconds
(Get-ArrayListForeachObjectTest -int $array).TotalMilliseconds
(Get-ArrayListScriptBlockTest -int $array).TotalMilliseconds
(Get-ArrayListFilterTest -int $array).TotalMilliseconds
""
}

ホスト出力

Write-Outputでオブジェクト出力のコードです

function Measure-Stopwatch{

    [CmdletBinding()]
    param(
    [parameter(Mandatory=$true)]
    [ScriptBlock]$Command,
    [switch]$Days,
    [switch]$Hours,
    [switch]$Minutes,
    [switch]$Seconds,
    [switch]$Milliseconds
    )

    function Start-InputScriptBlock {
        $sw = New-Object System.Diagnostics.StopWatch
    
        # Start Stopwatch
        $sw.Start()

        #TargetCommand to measure
        $command.Invoke()
    
        # Stop Stopwatch
        $sw.Stop()
    
        #Show Result
        switch ($true){
        $Days {$sw.Elapsed.TotalDays}
        $Hours {$sw.Elapsed.TotalHours}
        $Minutes {$sw.Elapsed.TotalMinutes}
        $Seconds {$sw.Elapsed.TotalSeconds}
        $Milliseconds {$sw.Elapsed.TotalMilliseconds}
        default {$sw.Elapsed}
        }

        #Reset Result
        $sw.Reset()
    }

    Start-InputScriptBlock
}

filter Get-FilterTest{

        [decimal]$total += $_
        Write-Output $total
}

function Get-intForTest{

    param(
    $int
    )

    Measure-Stopwatch -Command {
        for ($i = 0; $i -lt $int.length; $i++)
        {
            [decimal]$total += $int[$i]
            Write-Output $total
        }
    } -Milliseconds

}

function Get-intForeachTest{

    param(
    $int
    )

    Measure-Stopwatch -Command {
        foreach ($i in $int)
        {
            [decimal]$total += $i
            Write-Output $total
        }
    } -Milliseconds

}

function Get-intForeachObjectTest{

    param(
    $int
    )

    Measure-Stopwatch -Command{
        $int | ForEach-Object{
            [decimal]$total += $_
            Write-Output $total
        }
    } -Milliseconds
}

function Get-intScriptBlockTest{

    param(
    $int
    )

    Measure-Stopwatch -Command{
        $int | &{
            process{
                [decimal]$total += $_
                Write-Output $total
            }
        }
    } -Milliseconds
}

function Get-intFilterTest{

    param(
    $int
    )

    Measure-Stopwatch -Command{
        $int | Get-FilterTest
    } -Milliseconds
}

function Get-ListForTest{

	param(
	$int
	)

	$list  = New-Object 'System.Collections.Generic.List`1[System.String]'
 
	$int | foreach-Object { $list.Add($_)}

	#,$list

    Measure-Stopwatch -Command{
	    for ($i = 0; $i -lt $int.length; $i++)
        {
    	    [decimal]$total += $list[$i]
            Write-Output $total
        }
    } -Milliseconds
}

function Get-ListForeachTest{

	param(
	$int
	)

	$list  = New-Object 'System.Collections.Generic.List`1[System.String]'
 
	$int | foreach-Object { $list.Add($_)}

	#,$list

    Measure-Stopwatch -Command{
	    foreach ($l in $list)
        {
    	    [decimal]$total += $l
            Write-Output $total
        }
    } -Milliseconds
}

function Get-ListForeachObjectTest{

	param(
	$int
	)

	$list  = New-Object 'System.Collections.Generic.List`1[System.String]'
 
	$int | foreach-Object { $list.Add($_)}

	#,$list

    Measure-Stopwatch -Command{
	    $list | foreach-Object{
    	    [decimal]$total += $_
            Write-Output $total
        }
    } -Milliseconds
}

function Get-ListScriptBlockTest{

	param(
	$int
	)

	$list  = New-Object 'System.Collections.Generic.List`1[System.String]'
 
	$int | foreach-Object { $list.Add($_)}

	#,$list

    Measure-Stopwatch -Command{
	    $list | &{
            process{
                [decimal]$total += $_
                Write-Output $total
            }
        }
    } -Milliseconds
}

function Get-ListFilterTest{
    param(
    $int
    )	

	$list  = New-Object 'System.Collections.Generic.List`1[System.String]'
 
	$int | foreach-Object { $list.Add($_)}

	#,$list

    Measure-Stopwatch -Command{    
        $list | Get-FilterTest
    } -Milliseconds
}


function Get-ArrayListForTest{
    param(
    $int
    )
	
	$arrayList = New-Object System.Collections.ArrayList
	
	[Void]($int | foreach-Object { $arrayList.Add($_) })
 
	#,$Arraylist
	
    Measure-Stopwatch -Command{
	    for ($i = 0; $i -lt $int.length; $i++)
        {
    	    [decimal]$total += $arrayList[$i]
            Write-Output $total
        }
    } -Milliseconds
}

function Get-ArrayListForeachTest{
    param(
    $int
    )
	
	$arrayList = New-Object System.Collections.ArrayList
	
	[Void]($int | foreach-Object { $arrayList.Add($_) })
 
	#,$Arraylist
	
    Measure-Stopwatch -Command{
	    foreach ($al in $arrayList)
        {
    	    [decimal]$total += $al
            Write-Output $total
        }
    } -Milliseconds
}

function Get-ArrayListForeachObjectTest{
    param(
    $int
    )
	
	$arrayList = New-Object System.Collections.ArrayList
	
	[Void]($int | foreach-Object { $arrayList.Add($_) })
 
	#,$Arraylist
	
    Measure-Stopwatch -Command{
	    $arrayList | Foreach-Object{
    	    [decimal]$total += $_
            Write-Output $total
        }
    } -Milliseconds
}

function Get-ArrayListScriptBlockTest{
    param(
    $int
    )
	
	$arrayList = New-Object System.Collections.ArrayList
	
	[Void]($int | foreach-Object { $arrayList.Add($_) })
 
	#,$Arraylist
	
    Measure-Stopwatch -Command{
	    $arrayList | &{
            process{
                [decimal]$total += $_
                Write-Output $total
            }
        }
    } -Milliseconds
}

filter Get-ArrayListFilterTest{
    param(
    $int
    )	
    
	$arrayList = New-Object System.Collections.ArrayList
	
	[Void]($int | foreach-Object { $arrayList.Add($_) })
 
	#,$Arraylist
	Measure-Stopwatch -Command {
	    $arrayList | Get-FilterTest
    } -Milliseconds
}

[int[]]$array=@()
#$array += 1..30000000
$array += 1..10000

$result=@()


1..4 | ForEach-Object {
$result += Get-intForTest -int $array | select -last 1
$result += Get-intForeachTest -int $array | select -last 1
$result += Get-intForeachObjectTest -int $array | select -last 1
$result += Get-intScriptBlockTest -int $array | select -last 1
$result += Get-intFilterTest -int $array | select -last 1

$result += Get-ListForTest -int $array | select -last 1
$result += Get-ListForeachTest -int $array | select -last 1
$result += Get-ListForeachObjectTest -int $array | select -last 1
$result += Get-ListScriptBlockTest -int $array | select -last 1
$result += Get-ListFilterTest -int $array | select -last 1

$result += Get-ArrayListForTest -int $array | select -last 1
$result += Get-ArrayListForeachTest -int $array | select -last 1
$result += Get-ArrayListForeachObjectTest -int $array | select -last 1
$result += Get-ArrayListScriptBlockTest -int $array | select -last 1
$result += Get-ArrayListFilterTest -int $array | select -last 1
$result += ""
}
$result

Write-Outputでホスト出力のコードです

function Measure-Stopwatch{

    [CmdletBinding()]
    param(
    [parameter(Mandatory=$true)]
    [ScriptBlock]$Command,
    [switch]$Days,
    [switch]$Hours,
    [switch]$Minutes,
    [switch]$Seconds,
    [switch]$Milliseconds
    )

    function Start-InputScriptBlock {
        $sw = New-Object System.Diagnostics.StopWatch
    
        # Start Stopwatch
        $sw.Start()

        #TargetCommand to measure
        $command.Invoke()
    
        # Stop Stopwatch
        $sw.Stop()
    
        #Show Result
        switch ($true){
        $Days {$sw.Elapsed.TotalDays}
        $Hours {$sw.Elapsed.TotalHours}
        $Minutes {$sw.Elapsed.TotalMinutes}
        $Seconds {$sw.Elapsed.TotalSeconds}
        $Milliseconds {$sw.Elapsed.TotalMilliseconds}
        default {$sw.Elapsed}
        }

        #Reset Result
        $sw.Reset()
    }

    Start-InputScriptBlock
}

filter Get-FilterTest{

        [decimal]$total += $_
        Write-Output $total
}

function Get-intForTest{

    param(
    $int
    )

    Measure-Stopwatch -Command {
        for ($i = 0; $i -lt $int.length; $i++)
        {
            [decimal]$total += $int[$i]
            Write-Output $total
        }
    } 

}

function Get-intForeachTest{

    param(
    $int
    )

    Measure-Stopwatch -Command {
        foreach ($i in $int)
        {
            [decimal]$total += $i
            Write-Output $total
        }
    } 

}

function Get-intForeachObjectTest{

    param(
    $int
    )

    Measure-Stopwatch -Command{
        $int | ForEach-Object{
            [decimal]$total += $_
            Write-Output $total
        }
    } 
}

function Get-intScriptBlockTest{

    param(
    $int
    )

    Measure-Stopwatch -Command{
        $int | &{
            process{
                [decimal]$total += $_
                Write-Output $total
            }
        }
    } 
}

function Get-intFilterTest{

    param(
    $int
    )

    Measure-Stopwatch -Command{
        $int | Get-FilterTest
    } 
}

function Get-ListForTest{

	param(
	$int
	)

	$list  = New-Object 'System.Collections.Generic.List`1[System.String]'
 
	$int | foreach-Object { $list.Add($_)}

	#,$list

    Measure-Stopwatch -Command{
	    for ($i = 0; $i -lt $int.length; $i++)
        {
    	    [decimal]$total += $list[$i]
            Write-Output $total
        }
    } 
}

function Get-ListForeachTest{

	param(
	$int
	)

	$list  = New-Object 'System.Collections.Generic.List`1[System.String]'
 
	$int | foreach-Object { $list.Add($_)}

	#,$list

    Measure-Stopwatch -Command{
	    foreach ($l in $list)
        {
    	    [decimal]$total += $l
            Write-Output $total
        }
    } 
}

function Get-ListForeachObjectTest{

	param(
	$int
	)

	$list  = New-Object 'System.Collections.Generic.List`1[System.String]'
 
	$int | foreach-Object { $list.Add($_)}

	#,$list

    Measure-Stopwatch -Command{
	    $list | foreach-Object{
    	    [decimal]$total += $_
            Write-Output $total
        }
    } 
}

function Get-ListScriptBlockTest{

	param(
	$int
	)

	$list  = New-Object 'System.Collections.Generic.List`1[System.String]'
 
	$int | foreach-Object { $list.Add($_)}

	#,$list

    Measure-Stopwatch -Command{
	    $list | &{
            process{
                [decimal]$total += $_
                Write-Output $total
            }
        }
    } 
}

function Get-ListFilterTest{
    param(
    $int
    )	

	$list  = New-Object 'System.Collections.Generic.List`1[System.String]'
 
	$int | foreach-Object { $list.Add($_)}

	#,$list

    Measure-Stopwatch -Command{    
        $list | Get-FilterTest
    } 
}


function Get-ArrayListForTest{
    param(
    $int
    )
	
	$arrayList = New-Object System.Collections.ArrayList
	
	[Void]($int | foreach-Object { $arrayList.Add($_) })
 
	#,$Arraylist
	
    Measure-Stopwatch -Command{
	    for ($i = 0; $i -lt $int.length; $i++)
        {
    	    [decimal]$total += $arrayList[$i]
            Write-Output $total
        }
    } 
}

function Get-ArrayListForeachTest{
    param(
    $int
    )
	
	$arrayList = New-Object System.Collections.ArrayList
	
	[Void]($int | foreach-Object { $arrayList.Add($_) })
 
	#,$Arraylist
	
    Measure-Stopwatch -Command{
	    foreach ($al in $arrayList)
        {
    	    [decimal]$total += $al
            Write-Output $total
        }
    } 
}

function Get-ArrayListForeachObjectTest{
    param(
    $int
    )
	
	$arrayList = New-Object System.Collections.ArrayList
	
	[Void]($int | foreach-Object { $arrayList.Add($_) })
 
	#,$Arraylist
	
    Measure-Stopwatch -Command{
	    $arrayList | Foreach-Object{
    	    [decimal]$total += $_
            Write-Output $total
        }
    } 
}

function Get-ArrayListScriptBlockTest{
    param(
    $int
    )
	
	$arrayList = New-Object System.Collections.ArrayList
	
	[Void]($int | foreach-Object { $arrayList.Add($_) })
 
	#,$Arraylist
	
    Measure-Stopwatch -Command{
	    $arrayList | &{
            process{
                [decimal]$total += $_
                Write-Output $total
            }
        }
    } 
}

filter Get-ArrayListFilterTest{
    param(
    $int
    )	
    
	$arrayList = New-Object System.Collections.ArrayList
	
	[Void]($int | foreach-Object { $arrayList.Add($_) })
 
	#,$Arraylist
	Measure-Stopwatch -Command {
	    $arrayList | Get-FilterTest
    } 
}

[int[]]$array=@()
#$array += 1..30000000
$array += 1..10000

$result=@()


1 | ForEach-Object {
Get-intForTest -int $array
Get-intForeachTest -int $array
Get-intForeachObjectTest -int $array
Get-intScriptBlockTest -int $array
Get-intFilterTest -int $array

Get-ListForTest -int $array
Get-ListForeachTest -int $array
Get-ListForeachObjectTest -int $array
Get-ListScriptBlockTest -int $array
Get-ListFilterTest -int $array

Get-ArrayListForTest -int $array 
Get-ArrayListForeachTest -int $array 
Get-ArrayListForeachObjectTest -int $array 
Get-ArrayListScriptBlockTest -int $array 
Get-ArrayListFilterTest -int $array 
""
}

Write-Hostのコードです

filter Get-FilterTest{

        [decimal]$total += $_
        Write-Host $total
}

function Get-intForTest{

    param(
    $int
    )

    Measure-Command{
        for ($i = 0; $i -lt $int.length; $i++)
        {
            [decimal]$total += $int[$i]
            Write-Host $total
        }
    }

}

function Get-intForeachTest{

    param(
    $int
    )

    Measure-Command{
        foreach ($i in $int)
        {
            [decimal]$total += $i
            Write-Host $total
        }
    }

}

function Get-intForeachObjectTest{

    param(
    $int
    )

    Measure-Command{
        $int | ForEach-Object{
            [decimal]$total += $_
            Write-Host $total
        }
    }

}

function Get-intScriptBlockTest{

    param(
    $int
    )

    Measure-Command{
        $int | &{
            process{
                [decimal]$total += $_
                Write-Host $total
            }
        }
    }
}

function Get-intFilterTest{

    param(
    $int
    )

    Measure-Command{
        $int | Get-FilterTest
    }
}

function Get-ListForTest{

	param(
	$int
	)

	$list  = New-Object 'System.Collections.Generic.List`1[System.String]'

	$int | foreach-Object { $list.Add($_)}

	#,$list

    Measure-Command{
	    for ($i = 0; $i -lt $int.length; $i++)
        {
    	    [decimal]$total += $list[$i]
            Write-Host $total
        }
    }
}

function Get-ListForeachTest{

	param(
	$int
	)

	$list  = New-Object 'System.Collections.Generic.List`1[System.String]'

	$int | foreach-Object { $list.Add($_)}

	#,$list

    Measure-Command{
	    foreach ($l in $list)
        {
    	    [decimal]$total += $l
            Write-Host $total
        }
    }
}

function Get-ListForeachObjectTest{

	param(
	$int
	)

	$list  = New-Object 'System.Collections.Generic.List`1[System.String]'

	$int | foreach-Object { $list.Add($_)}

	#,$list

    Measure-Command{
	    $list | foreach-Object{
    	    [decimal]$total += $_
            Write-Host $total
        }
    }
}

function Get-ListScriptBlockTest{

	param(
	$int
	)

	$list  = New-Object 'System.Collections.Generic.List`1[System.String]'

	$int | foreach-Object { $list.Add($_)}

	#,$list

    Measure-Command{
	    $list | &{
            process{
                [decimal]$total += $_
                Write-Host $total
            }
        }
    }
}

function Get-ListFilterTest{
    param(
    $int
    )

	$list  = New-Object 'System.Collections.Generic.List`1[System.String]'

	$int | foreach-Object { $list.Add($_)}

	#,$list

    Measure-Command{
        $list | Get-FilterTest
    }
}

function Get-ArrayListForTest{
    param(
    $int
    )

	$arrayList = New-Object System.Collections.ArrayList

	[Void]($int | foreach-Object { $arrayList.Add($_) })

	#,$Arraylist

    Measure-Command{
	    for ($i = 0; $i -lt $int.length; $i++)
        {
    	    [decimal]$total += $arrayList[$i]
            Write-Host $total
        }
    }
}

function Get-ArrayListForeachTest{
    param(
    $int
    )

	$arrayList = New-Object System.Collections.ArrayList

	[Void]($int | foreach-Object { $arrayList.Add($_) })

	#,$Arraylist

    Measure-Command{
	    foreach ($al in $arrayList)
        {
    	    [decimal]$total += $al
            Write-Host $total
        }
    }
}

function Get-ArrayListForeachObjectTest{
    param(
    $int
    )

	$arrayList = New-Object System.Collections.ArrayList

	[Void]($int | foreach-Object { $arrayList.Add($_) })

	#,$Arraylist

    Measure-Command{
	    $arrayList | Foreach-Object{
    	    [decimal]$total += $_
            Write-Host $total
        }
    }
}

function Get-ArrayListScriptBlockTest{
    param(
    $int
    )

	$arrayList = New-Object System.Collections.ArrayList

	[Void]($int | foreach-Object { $arrayList.Add($_) })

	#,$Arraylist

    Measure-Command{
	    $arrayList | &{
            process{
                [decimal]$total += $_
                Write-Host $total
            }
        }
    }
}

filter Get-ArrayListFilterTest{
    param(
    $int
    )

	$arrayList = New-Object System.Collections.ArrayList

	[Void]($int | foreach-Object { $arrayList.Add($_) })

	#,$Arraylist
	Measure-Command {
	    $arrayList | Get-FilterTest
    }
}

[int[]]$array=@()
#$array += 1..30000000
$array += 1..10000

$result=@()

1..4 | ForEach-Object {
$result += (Get-intForTest -int $array).TotalMilliseconds | Select -Last 1
$result += (Get-intForeachTest -int $array).TotalMilliseconds | Select -Last 1
$result += (Get-intForeachObjectTest -int $array).TotalMilliseconds | Select -Last 1
$result += (Get-intScriptBlockTest -int $array).TotalMilliseconds | Select -Last 1
$result += (Get-intFilterTest -int $array).TotalMilliseconds | Select -Last 1

$result += (Get-ListForTest -int $array).TotalMilliseconds | Select -Last 1
$result += (Get-ListForeachTest -int $array).TotalMilliseconds | Select -Last 1
$result += (Get-ListForeachObjectTest -int $array).TotalMilliseconds | Select -Last 1
$result += (Get-ListScriptBlockTest -int $array).TotalMilliseconds | Select -Last 1
$result += (Get-ListFilterTest -int $array).TotalMilliseconds | Select -Last 1

$result += (Get-ArrayListForTest -int $array).TotalMilliseconds | Select -Last 1
$result += (Get-ArrayListForeachTest -int $array).TotalMilliseconds | Select -Last 1
$result += (Get-ArrayListForeachObjectTest -int $array).TotalMilliseconds | Select -Last 1
$result += (Get-ArrayListScriptBlockTest -int $array).TotalMilliseconds | Select -Last 1
$result += (Get-ArrayListFilterTest -int $array).TotalMilliseconds | Select -Last 1
$result += ""
}
$result

[System.Console]::Console()のコードです

filter Get-FilterTest{

        [decimal]$total += $_
        [console]::WriteLine("$total")
}

function Get-intForTest{

    param(
    $int
    )

    Measure-Command{
        for ($i = 0; $i -lt $int.length; $i++)
        {
            [decimal]$total += $int[$i]
            [console]::WriteLine("$total")
        }
    }

}

function Get-intForeachTest{

    param(
    $int
    )

    Measure-Command{
        foreach ($i in $int)
        {
            [decimal]$total += $i
            [console]::WriteLine("$total")
        }
    }

}

function Get-intForeachObjectTest{

    param(
    $int
    )

    Measure-Command{
        $int | ForEach-Object{
            [decimal]$total += $_
            [console]::WriteLine("$total")
        }
    }

}

function Get-intScriptBlockTest{

    param(
    $int
    )

    Measure-Command{
        $int | &{
            process{
                [decimal]$total += $_
                [console]::WriteLine("$total")
            }
        }
    }
}

function Get-intFilterTest{

    param(
    $int
    )

    Measure-Command{
        $int | Get-FilterTest
    }
}

function Get-ListForTest{

	param(
	$int
	)

	$list  = New-Object 'System.Collections.Generic.List`1[System.String]'

	$int | foreach-Object { $list.Add($_)}

	#,$list

    Measure-Command{
	    for ($i = 0; $i -lt $int.length; $i++)
        {
    	    [decimal]$total += $list[$i]
            [console]::WriteLine("$total")
        }
    }
}

function Get-ListForeachTest{

	param(
	$int
	)

	$list  = New-Object 'System.Collections.Generic.List`1[System.String]'

	$int | foreach-Object { $list.Add($_)}

	#,$list

    Measure-Command{
	    foreach ($l in $list)
        {
    	    [decimal]$total += $l
            [console]::WriteLine("$total")
        }
    }
}

function Get-ListForeachObjectTest{

	param(
	$int
	)

	$list  = New-Object 'System.Collections.Generic.List`1[System.String]'

	$int | foreach-Object { $list.Add($_)}

	#,$list

    Measure-Command{
	    $list | foreach-Object{
    	    [decimal]$total += $_
            [console]::WriteLine("$total")
        }
    }
}

function Get-ListScriptBlockTest{

	param(
	$int
	)

	$list  = New-Object 'System.Collections.Generic.List`1[System.String]'

	$int | foreach-Object { $list.Add($_)}

	#,$list

    Measure-Command{
	    $list | &{
            process{
                [decimal]$total += $_
                [console]::WriteLine("$total")
            }
        }
    }
}

function Get-ListFilterTest{
    param(
    $int
    )

	$list  = New-Object 'System.Collections.Generic.List`1[System.String]'

	$int | foreach-Object { $list.Add($_)}

	#,$list

    Measure-Command{
        $list | Get-FilterTest
    }
}

function Get-ArrayListForTest{
    param(
    $int
    )

	$arrayList = New-Object System.Collections.ArrayList

	[Void]($int | foreach-Object { $arrayList.Add($_) })

	#,$Arraylist

    Measure-Command{
	    for ($i = 0; $i -lt $int.length; $i++)
        {
    	    [decimal]$total += $arrayList[$i]
            [console]::WriteLine("$total")
        }
    }
}

function Get-ArrayListForeachTest{
    param(
    $int
    )

	$arrayList = New-Object System.Collections.ArrayList

	[Void]($int | foreach-Object { $arrayList.Add($_) })

	#,$Arraylist

    Measure-Command{
	    foreach ($al in $arrayList)
        {
    	    [decimal]$total += $al
            [console]::WriteLine("$total")
        }
    }
}

function Get-ArrayListForeachObjectTest{
    param(
    $int
    )

	$arrayList = New-Object System.Collections.ArrayList

	[Void]($int | foreach-Object { $arrayList.Add($_) })

	#,$Arraylist

    Measure-Command{
	    $arrayList | Foreach-Object{
    	    [decimal]$total += $_
            [console]::WriteLine("$total")
        }
    }
}

function Get-ArrayListScriptBlockTest{
    param(
    $int
    )

	$arrayList = New-Object System.Collections.ArrayList

	[Void]($int | foreach-Object { $arrayList.Add($_) })

	#,$Arraylist

    Measure-Command{
	    $arrayList | &{
            process{
                [decimal]$total += $_
                [console]::WriteLine("$total")
            }
        }
    }
}

filter Get-ArrayListFilterTest{
    param(
    $int
    )

	$arrayList = New-Object System.Collections.ArrayList

	[Void]($int | foreach-Object { $arrayList.Add($_) })

	#,$Arraylist
	Measure-Command {
	    $arrayList | Get-FilterTest
    }
}

[int[]]$array=@()
#$array += 1..30000000
$array += 1..10000

$result=@()

1..4 | ForEach-Object {
$result += (Get-intForTest -int $array).TotalMilliseconds | Select -Last 1
$result += (Get-intForeachTest -int $array).TotalMilliseconds | Select -Last 1
$result += (Get-intForeachObjectTest -int $array).TotalMilliseconds | Select -Last 1
$result += (Get-intScriptBlockTest -int $array).TotalMilliseconds | Select -Last 1
$result += (Get-intFilterTest -int $array).TotalMilliseconds | Select -Last 1

$result += (Get-ListForTest -int $array).TotalMilliseconds | Select -Last 1
$result += (Get-ListForeachTest -int $array).TotalMilliseconds | Select -Last 1
$result += (Get-ListForeachObjectTest -int $array).TotalMilliseconds | Select -Last 1
$result += (Get-ListScriptBlockTest -int $array).TotalMilliseconds | Select -Last 1
$result += (Get-ListFilterTest -int $array).TotalMilliseconds | Select -Last 1

$result += (Get-ArrayListForTest -int $array).TotalMilliseconds | Select -Last 1
$result += (Get-ArrayListForeachTest -int $array).TotalMilliseconds | Select -Last 1
$result += (Get-ArrayListForeachObjectTest -int $array).TotalMilliseconds | Select -Last 1
$result += (Get-ArrayListScriptBlockTest -int $array).TotalMilliseconds | Select -Last 1
$result += (Get-ArrayListFilterTest -int $array).TotalMilliseconds | Select -Last 1
$result += ""
}
$result

ファイル出力

Add-Contentのコードです

if ((Test-Path -Path .\Temp) -eq $false)
{
    New-Item -ItemType Directory -Path .\Temp
}
elseif ((Test-Path -Path .\Temp\test.txt) -eq $true)
{
    Remove-Item .\temp\test.txt
}

filter Get-FilterTest{

        [decimal]$total += $_
        Add-Content -Value $total -Path .\Temp\test.txt
}

function Get-intForTest{

    param(
    $int
    )

    Measure-Command{
        for ($i = 0; $i -lt $int.length; $i++)
        {
            [decimal]$total += $int[$i]
            Add-Content -Value $total -Path .\Temp\test.txt
        }
    }
}

function Get-intForeachTest{

    param(
    $int
    )

    Measure-Command{
        foreach ($i in $int)
        {
            [decimal]$total += $i
            Add-Content -Value $total -Path .\Temp\test.txt
        }
    }
}

function Get-intForeachObjectTest{

    param(
    $int
    )

    Measure-Command{
        $int | ForEach-Object{
            [decimal]$total += $_
            Add-Content -Value $total -Path .\Temp\test.txt
        }
    }
}

function Get-intScriptBlockTest{

    param(
    $int
    )

    Measure-Command{
        $int | &{
            process{
                [decimal]$total += $_
                Add-Content -Value $total -Path .\Temp\test.txt
            }
        }
    }
}

function Get-intFilterTest{

    param(
    $int
    )

    Measure-Command{
        $int | Get-FilterTest
    }
}

function Get-ListForTest{

	param(
	$int
	)

	$list  = New-Object 'System.Collections.Generic.List`1[System.String]'

	$int | foreach-Object { $list.Add($_)}

	#,$list

    Measure-Command{
	    for ($i = 0; $i -lt $int.length; $i++)
        {
    	    [decimal]$total += $list[$i]
            Add-Content -Value $total -Path .\Temp\test.txt
        }
    }
}

function Get-ListForeachTest{

	param(
	$int
	)

	$list  = New-Object 'System.Collections.Generic.List`1[System.String]'

	$int | foreach-Object { $list.Add($_)}

	#,$list

    Measure-Command{
	    foreach ($l in $list)
        {
    	    [decimal]$total += $l
            Add-Content -Value $total -Path .\Temp\test.txt
        }
    }
}

function Get-ListForeachObjectTest{

	param(
	$int
	)

	$list  = New-Object 'System.Collections.Generic.List`1[System.String]'

	$int | foreach-Object { $list.Add($_)}

	#,$list

    Measure-Command{
	    $list | foreach-Object{
    	    [decimal]$total += $_
            Add-Content -Value $total -Path .\Temp\test.txt
        }
    }
}

function Get-ListScriptBlockTest{

	param(
	$int
	)

	$list  = New-Object 'System.Collections.Generic.List`1[System.String]'

	$int | foreach-Object { $list.Add($_)}

	#,$list

    Measure-Command{
	    $list | &{
            process{
                [decimal]$total += $_
                Add-Content -Value $total -Path .\Temp\test.txt
            }
        }
    }
}

function Get-ListFilterTest{
    param(
    $int
    )

	$list  = New-Object 'System.Collections.Generic.List`1[System.String]'

	$int | foreach-Object { $list.Add($_)}

	#,$list

    Measure-Command{
        $list | Get-FilterTest
    }
}

function Get-ArrayListForTest{
    param(
    $int
    )

	$arrayList = New-Object System.Collections.ArrayList

	[Void]($int | foreach-Object { $arrayList.Add($_) })

	#,$Arraylist

    Measure-Command{
	    for ($i = 0; $i -lt $int.length; $i++)
        {
    	    [decimal]$total += $arrayList[$i]
            Add-Content -Value $total -Path .\Temp\test.txt
        }
    }
}

function Get-ArrayListForeachTest{
    param(
    $int
    )

	$arrayList = New-Object System.Collections.ArrayList

	[Void]($int | foreach-Object { $arrayList.Add($_) })

	#,$Arraylist

    Measure-Command{
	    foreach ($al in $arrayList)
        {
    	    [decimal]$total += $al
            Add-Content -Value $total -Path .\Temp\test.txt
        }
    }
}

function Get-ArrayListForeachObjectTest{
    param(
    $int
    )

	$arrayList = New-Object System.Collections.ArrayList

	[Void]($int | foreach-Object { $arrayList.Add($_) })

	#,$Arraylist

    Measure-Command{
	    $arrayList | Foreach-Object{
    	    [decimal]$total += $_
            Add-Content -Value $total -Path .\Temp\test.txt
        }
    }
}

function Get-ArrayListScriptBlockTest{
    param(
    $int
    )

	$arrayList = New-Object System.Collections.ArrayList

	[Void]($int | foreach-Object { $arrayList.Add($_) })

	#,$Arraylist

    Measure-Command{
	    $arrayList | &{
            process{
                [decimal]$total += $_
                Add-Content -Value $total -Path .\Temp\test.txt
            }
        }
    }
}

filter Get-ArrayListFilterTest{
    param(
    $int
    )

	$arrayList = New-Object System.Collections.ArrayList

	[Void]($int | foreach-Object { $arrayList.Add($_) })

	#,$Arraylist
	Measure-Command {
	    $arrayList | Get-FilterTest
    }
}

[int[]]$array=@()
#$array += 1..30000000
$array += 1..10000

1..4 | ForEach-Object {
(Get-intForTest -int $array).TotalMilliseconds
Remove-Item .\temp\test.txt
sleep -Seconds 5

(Get-intForeachTest -int $array).TotalMilliseconds
Remove-Item .\temp\test.txt
sleep -Seconds 5

(Get-intForeachObjectTest -int $array).TotalMilliseconds
Remove-Item .\temp\test.txt
sleep -Seconds 5

(Get-intScriptBlockTest -int $array).TotalMilliseconds
Remove-Item .\temp\test.txt
sleep -Seconds 5

(Get-intFilterTest -int $array).TotalMilliseconds
Remove-Item .\temp\test.txt
sleep -Seconds 5

(Get-ListForTest -int $array).TotalMilliseconds
Remove-Item .\temp\test.txt
sleep -Seconds 5

(Get-ListForeachTest -int $array).TotalMilliseconds
Remove-Item .\temp\test.txt
sleep -Seconds 5

(Get-ListForeachObjectTest -int $array).TotalMilliseconds
Remove-Item .\temp\test.txt
sleep -Seconds 5

(Get-ListScriptBlockTest -int $array).TotalMilliseconds
Remove-Item .\temp\test.txt
sleep -Seconds 5

(Get-ListFilterTest -int $array).TotalMilliseconds
Remove-Item .\temp\test.txt
sleep -Seconds 5

(Get-ArrayListForTest -int $array).TotalMilliseconds
Remove-Item .\temp\test.txt
sleep -Seconds 5

(Get-ArrayListForeachTest -int $array).TotalMilliseconds
Remove-Item .\temp\test.txt
sleep -Seconds 5

(Get-ArrayListForeachObjectTest -int $array).TotalMilliseconds
Remove-Item .\temp\test.txt
sleep -Seconds 5

(Get-ArrayListScriptBlockTest -int $array).TotalMilliseconds
Remove-Item .\temp\test.txt
sleep -Seconds 5

(Get-ArrayListFilterTest -int $array).TotalMilliseconds
Remove-Item .\temp\test.txt
sleep -Seconds 5
""
}

Out-Fileのコードです

if ((Test-Path -Path .\Temp) -eq $false)
{
    New-Item -ItemType Directory -Path .\Temp
}
elseif ((Test-Path -Path .\Temp\test.txt) -eq $true)
{
    Remove-Item .\temp\test.txt
}

filter Get-FilterTest{

        [decimal]$total += $_
        $total | Out-File -FilePath .\Temp\test.txt -Append
}

function Get-intForTest{

    param(
    $int
    )

    Measure-Command{
        for ($i = 0; $i -lt $int.length; $i++)
        {
            [decimal]$total += $int[$i]
            $total | Out-File -FilePath .\Temp\test.txt -Append
        }
    }
}

function Get-intForeachTest{

    param(
    $int
    )

    Measure-Command{
        foreach ($i in $int)
        {
            [decimal]$total += $i
            $total | Out-File -FilePath .\Temp\test.txt -Append
        }
    }
}

function Get-intForeachObjectTest{

    param(
    $int
    )

    Measure-Command{
        $int | ForEach-Object{
            [decimal]$total += $_
            $total | Out-File -FilePath .\Temp\test.txt -Append
        }
    }
}

function Get-intScriptBlockTest{

    param(
    $int
    )

    Measure-Command{
        $int | &{
            process{
                [decimal]$total += $_
                $total | Out-File -FilePath .\Temp\test.txt -Append
            }
        }
    }
}

function Get-intFilterTest{

    param(
    $int
    )

    Measure-Command{
        $int | Get-FilterTest
    }
}

function Get-ListForTest{

	param(
	$int
	)

	$list  = New-Object 'System.Collections.Generic.List`1[System.String]'

	$int | foreach-Object { $list.Add($_)}

	#,$list

    Measure-Command{
	    for ($i = 0; $i -lt $int.length; $i++)
        {
    	    [decimal]$total += $list[$i]
            $total | Out-File -FilePath .\Temp\test.txt -Append
        }
    }
}

function Get-ListForeachTest{

	param(
	$int
	)

	$list  = New-Object 'System.Collections.Generic.List`1[System.String]'

	$int | foreach-Object { $list.Add($_)}

	#,$list

    Measure-Command{
	    foreach ($l in $list)
        {
    	    [decimal]$total += $l
            $total | Out-File -FilePath .\Temp\test.txt -Append
        }
    }
}

function Get-ListForeachObjectTest{

	param(
	$int
	)

	$list  = New-Object 'System.Collections.Generic.List`1[System.String]'

	$int | foreach-Object { $list.Add($_)}

	#,$list

    Measure-Command{
	    $list | foreach-Object{
    	    [decimal]$total += $_
            $total | Out-File -FilePath .\Temp\test.txt -Append
        }
    }
}

function Get-ListScriptBlockTest{

	param(
	$int
	)

	$list  = New-Object 'System.Collections.Generic.List`1[System.String]'

	$int | foreach-Object { $list.Add($_)}

	#,$list

    Measure-Command{
	    $list | &{
            process{
                [decimal]$total += $_
                $total | Out-File -FilePath .\Temp\test.txt -Append
            }
        }
    }
}

function Get-ListFilterTest{
    param(
    $int
    )

	$list  = New-Object 'System.Collections.Generic.List`1[System.String]'

	$int | foreach-Object { $list.Add($_)}

	#,$list

    Measure-Command{
        $list | Get-FilterTest
    }
}

function Get-ArrayListForTest{
    param(
    $int
    )

	$arrayList = New-Object System.Collections.ArrayList

	[Void]($int | foreach-Object { $arrayList.Add($_) })

	#,$Arraylist

    Measure-Command{
	    for ($i = 0; $i -lt $int.length; $i++)
        {
    	    [decimal]$total += $arrayList[$i]
            $total | Out-File -FilePath .\Temp\test.txt -Append
        }
    }
}

function Get-ArrayListForeachTest{
    param(
    $int
    )

	$arrayList = New-Object System.Collections.ArrayList

	[Void]($int | foreach-Object { $arrayList.Add($_) })

	#,$Arraylist

    Measure-Command{
	    foreach ($al in $arrayList)
        {
    	    [decimal]$total += $al
            $total | Out-File -FilePath .\Temp\test.txt -Append
        }
    }
}

function Get-ArrayListForeachObjectTest{
    param(
    $int
    )

	$arrayList = New-Object System.Collections.ArrayList

	[Void]($int | foreach-Object { $arrayList.Add($_) })

	#,$Arraylist

    Measure-Command{
	    $arrayList | Foreach-Object{
    	    [decimal]$total += $_
            $total | Out-File -FilePath .\Temp\test.txt -Append
        }
    }
}

function Get-ArrayListScriptBlockTest{
    param(
    $int
    )

	$arrayList = New-Object System.Collections.ArrayList

	[Void]($int | foreach-Object { $arrayList.Add($_) })

	#,$Arraylist

    Measure-Command{
	    $arrayList | &{
            process{
                [decimal]$total += $_
                $total | Out-File -FilePath .\Temp\test.txt -Append
            }
        }
    }
}

filter Get-ArrayListFilterTest{
    param(
    $int
    )

	$arrayList = New-Object System.Collections.ArrayList

	[Void]($int | foreach-Object { $arrayList.Add($_) })

	#,$Arraylist
	Measure-Command {
	    $arrayList | Get-FilterTest
    }
}

[int[]]$array=@()
#$array += 1..30000000
$array += 1..10000

1..4 | ForEach-Object {
(Get-intForTest -int $array).TotalMilliseconds
Remove-Item .\temp\test.txt
sleep -Seconds 5

(Get-intForeachTest -int $array).TotalMilliseconds
Remove-Item .\temp\test.txt
sleep -Seconds 5

(Get-intForeachObjectTest -int $array).TotalMilliseconds
Remove-Item .\temp\test.txt
sleep -Seconds 5

(Get-intScriptBlockTest -int $array).TotalMilliseconds
Remove-Item .\temp\test.txt
sleep -Seconds 5

(Get-intFilterTest -int $array).TotalMilliseconds
Remove-Item .\temp\test.txt
sleep -Seconds 5

(Get-ListForTest -int $array).TotalMilliseconds
Remove-Item .\temp\test.txt
sleep -Seconds 5

(Get-ListForeachTest -int $array).TotalMilliseconds
Remove-Item .\temp\test.txt
sleep -Seconds 5

(Get-ListForeachObjectTest -int $array).TotalMilliseconds
Remove-Item .\temp\test.txt
sleep -Seconds 5

(Get-ListScriptBlockTest -int $array).TotalMilliseconds
Remove-Item .\temp\test.txt
sleep -Seconds 5

(Get-ListFilterTest -int $array).TotalMilliseconds
Remove-Item .\temp\test.txt
sleep -Seconds 5

(Get-ArrayListForTest -int $array).TotalMilliseconds
Remove-Item .\temp\test.txt
sleep -Seconds 5

(Get-ArrayListForeachTest -int $array).TotalMilliseconds
Remove-Item .\temp\test.txt
sleep -Seconds 5

(Get-ArrayListForeachObjectTest -int $array).TotalMilliseconds
Remove-Item .\temp\test.txt
sleep -Seconds 5

(Get-ArrayListScriptBlockTest -int $array).TotalMilliseconds
Remove-Item .\temp\test.txt
sleep -Seconds 5

(Get-ArrayListFilterTest -int $array).TotalMilliseconds
Remove-Item .\temp\test.txt
sleep -Seconds 5
""
}

ライセンス

本記事のソースコードやファイルは、元サイト同様にThe Code Project Open License (CPOL)とします。