Last active
July 26, 2020 15:47
-
-
Save ay65535/68d968a2613fdd3f1b56f1c17b79fc4c to your computer and use it in GitHub Desktop.
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
# https://qiita.com/acuo/items/9928e1fbb31b238d9705#%E4%BB%8A%E5%9B%9E%E4%BD%BF%E3%81%A3%E3%81%9F%E3%82%B3%E3%83%BC%E3%83%89%E5%85%A8%E3%81%A6 | |
function Test($Scripts, $N) { | |
# 各スクリプトの並びをシャッフルしてN回試行しその平均タイムを取得 | |
$Scripts * $N | Sort-Object { Get-random } | | |
Select-Object Name, @{ name = "Time"; expression = { (Measure-Command $_.Script).TotalMilliseconds } } | | |
Group-Object Name | | |
Select-Object Name, @{ name = "AverageTime"; expression = { ($_.Group | Measure-Object Time -Average).Average } } | | |
Sort-Object AverageTime | | |
Format-Table Name, @{ label = "AverageTime(msec)"; expression = { $_.AverageTime -as "int" } } -AutoSize | |
} | |
#Import-Module ".\looptest.dll" -Cmdlet "Get-Twice" | |
# $src = @" | |
# using System; | |
# using System.Linq; | |
# public class LoopTest | |
# { | |
# public static int[] Twice(object[] array) | |
# { | |
# return array.Select(x => 2 * (int)x).ToArray(); | |
# } | |
# } | |
# "@ | |
# Add-Type -TypeDefinition $src -Language CSharp | |
filter TwiceFilter { 2 * $_.Where( { [IO.FileInfo] -eq $_.GetType() }).LastWriteTime.Second } | |
function TwiceFunction { process { 2 * $_.Where( { [IO.FileInfo] -eq $_.GetType() }).LastWriteTime.Second } } | |
function TwiceAdvance { param([Parameter(ValueFromPipeline)][IO.FileSystemInfo]$v) process { 2 * $v.Where( { [IO.FileInfo] -eq $v.GetType() }).LastWriteTime.Second } } | |
$foreachScripts = @( | |
[pscustomobject]@{ | |
Name = "ドットソース演算子(.)" | |
Script = { $array | . { process { 2 * $_.Where( { [IO.FileInfo] -eq $_.GetType() }).LastWriteTime.Second } } } | |
} | |
[pscustomobject]@{ | |
Name = "呼び出し演算子(&)" | |
Script = { $array | & { process { 2 * $_.Where( { [IO.FileInfo] -eq $_.GetType() }).LastWriteTime.Second } } } | |
} | |
[pscustomobject]@{ | |
Name = "filter" | |
Script = { $array | TwiceFilter } | |
} | |
[pscustomobject]@{ | |
Name = "関数" | |
Script = { $array | TwiceFunction } | |
} | |
[pscustomobject]@{ | |
Name = "高度な関数" | |
Script = { $array | TwiceAdvance } | |
} | |
[pscustomobject]@{ | |
Name = "ForEach-Object" | |
Script = { $array | ForEach-Object { 2 * $_.Where( { [IO.FileInfo] -eq $_.GetType() }).LastWriteTime.Second } } | |
} | |
[pscustomobject]@{ | |
Name = "ForEachメソッド" | |
Script = { $array.ForEach( { 2 * $_.Where( { [IO.FileInfo] -eq $_.GetType() }).LastWriteTime.Second }) } | |
} | |
[pscustomobject]@{ | |
Name = "foreach文" | |
Script = { foreach ($i in $array) { 2 * $i.Where( { [IO.FileInfo] -eq $i.GetType() }).LastWriteTime.Second } } | |
} | |
[pscustomobject]@{ | |
Name = "for文" | |
Script = { for ($i = 0; $i -lt $array.Length; $i++) { 2 * $array[$i].Where( { [IO.FileInfo] -eq $array[$i].GetType() }).LastWriteTime.Second } } | |
} | |
[pscustomobject]@{ | |
Name = "while文" | |
Script = { $i = 0; while ($i -lt $array.Length) { 2 * $array[$i].Where( { [IO.FileInfo] -eq $array[$i].GetType() }).LastWriteTime.Second; $i++ } } | |
} | |
[pscustomobject]@{ | |
Name = "switch文" | |
Script = { switch ($array) { default { 2 * $_.Where( { [IO.FileInfo] -eq $_.GetType() }).LastWriteTime.Second } } } | |
} | |
[pscustomobject]@{ | |
Name = "Select(LINQ)" | |
Script = { [System.Linq.Enumerable]::Select($array, [Func[object, IO.FileSystemInfo]] { param($i) 2 * $i.Where( { [IO.FileInfo] -eq $i.GetType() }).LastWriteTime.Second }) } | |
} | |
# [pscustomobject]@{ | |
# Name = "C# (Add-Type)" | |
# Script = { [LoopTest]::Twice($array) } | |
# } | |
#[pscustomobject]@{ | |
# Name = "コマンドレット(Binary Module)" | |
# Script = { $array | Get-Twice } | |
#} | |
) | |
$files = Get-ChildItem -Recurse "$env:ProgramFiles" | |
$files.Count | |
$array = $files | |
$type = $files[0].GetType().BaseType.Name | |
$n = 50 # ループ1種類辺り計測回数 | |
"*" * 60 | |
"各種ループでサイズ$($array.Count)の${type}型配列の各要素に2をかけた値を取得するテスト" | |
"${n}回ずつ計測して平均タイムをミリ秒で取得" | |
"*" * 60 | |
Test $foreachScripts $n |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment