English 中文(简体)
性能测试两种权力方法 5. 壳牌照分类
原标题:Performance testing two methods of PowerShell integer division
问题回答

就我而言,这种业绩优化并不真正重要。 权力 壳体本身比所编纂的兰经要慢得多,因此,如果你真的需要履约、使用所汇编的语言或用<条码”编辑你的代码的话。 Add-Type。

Besides that - if you test performance, you need minimal other code that could change the results. Foreach-Object itself adds its own complexity. That s why I would advice to use foreach statement instead.

很奇怪,我的机器上的结果是有时<>>。

[76]: $loopLength = 100000
[77]:
[77]: $runtime = Measure-Command {
>>     foreach($i in 1..$loopLength) {
>>         Foreach ($divisor in 2,3,5,7) {
>>             [Math]::Floor([int]$i / [int]$divisor) > $null
>>         }
>>     }
>> }
>>
[78]: "Double Cast: $($runtime.TotalMilliSeconds)"
Double Cast: 16294.3328
[79]:
[79]: $runtime = Measure-Command {
>>     foreach($i in 1..$loopLength) {
>>         Foreach ($divisor in 2,3,5,7) {
>>             [int][Math]::Floor($i / $divisor) > $null
>>         }
>>     }
>> }
>> "Single Cast: $($runtime.TotalMilliSeconds)"
>>
Single Cast: 15924.3836

I was also able to cut the time by about a factor of 7 from the ForEach example by just using good old-fashioned For loops; although the difference between the fastest and slowest was still only around 20 milliseconds.

不过,你还应指出,两个投放地点用于非专利投入的业务差别很小:

  • [Math]::Floor([int]17.2 / [int]1.1) = 17
  • [int][Math]::Floor(17.2 / 1.1) = 15

《普通法典》:

$Spins       = 100
$loopLength  = 10000
$Denominator = 2,3,5,7

$runtime = Measure-Command {
   for($s=0;$s -lt $Spins;$s++){
    for($Numerator=1;$Numerator -le $loopLength; $Numerator++){
        For($i=0;$i -lt 4;$i++) {
            $null=[Math]::Floor([int]$Numerator / [int]$Denominator[$i])
        }
    }
   }
}

"Double Cast: $($runtime.TotalMilliSeconds/$Spins)"

$runtime = Measure-Command {
   for($s=0;$s -lt $Spins;$s++){
    for($Numerator=1;$Numerator -le $loopLength; $Numerator++){
        For($i=0;$i -lt 4;$i++) {
            $null=[int][Math]::Floor($Numerator / $Denominator[$i])
        }
    }
   }
}
"Single Cast: $($runtime.TotalMilliSeconds/$Spins)"

add-type -TypeDefinition @ 
public class intOps{
   public static ulong div(ulong a, ulong b){
      return a/b;
   }
}
 @
$runtime = Measure-Command {
   for($s=0;$s -lt $Spins;$s++){
    for($Numerator=1;$Numerator -le $loopLength; $Numerator++){
        For($i=0;$i -lt 4;$i++) {
            $null=[intOps]::div($Numerator,$Denominator[$i])
        }
    }
   }
}
"C# Cast: $($runtime.TotalMilliSeconds/$Spins)"

技术网的例子不言而喻,因为编号已经为<条码>。 举一个例子:

PS C:Usersandy> [math]::floor( 100 / 26 ).GetType().Fullname
System.Double
PS C:Usersandy> (100).GetType().FullName
System.Int32
PS C:Usersandy> [int].FullName
System.Int32

So it s completely unnecessary to put [int] in front of the Floor method parameters because they are already of type System.Int32.

另外,你也不想将已退还的<代码>System.Double投到Int<32/code>上,因为收益价值可能大于。 Int32 can hold. 例如:

PS C:Usersandy> [int][math]::floor( ([int]::MaxValue + 1) / 1 )
Cannot convert value "2147483648" to type "System.Int32". Error: "Value was either too large or too small for an Int32."

就业绩而言,速度差异微不足道。 权力 壳牌发动机在幕后进行许多类型的适应和胁迫,无论你是否希望这样做。 这样做是为了使系统管理不得不对 in、双 double、 de等感到担忧。 有一些权利? 例如:

[Math]::Floor("123")
# This outputs 123 as System.Double.

甚至在C#中汇编。 该轮机运行时间为达到地面方法签名而进行必要的投放。

另一个例子:

"2" / "1"
# This outputs 2 as System.Int32.

充电灯塔是可能的,但电荷轮发动机确实在使你能够从事这项工作的背景下进行了转变。

这里是我的机器的性能结果:

function Get-SingleCastTime {
    $runtime = Measure-Command {
        1..10000 | ForEach-Object {
            Foreach ($divisor in 2,3,5,7) {
                [int][Math]::Floor($_ / $divisor) > $null          
            }
        }
    }
    "Single Cast: $($runtime.TotalMilliSeconds)"
}

function Get-DoubleCastTime {
    $runtime = Measure-Command {
        1..10000 | ForEach-Object {
            Foreach ($divisor in 2,3,5,7) {
                [Math]::Floor([int]$_ / [int]$divisor) > $null
            }
        }
    }

    "Double Cast: $($runtime.TotalMilliSeconds)"
}

Get-SingleCastTime
#Single Cast: 614.6537

Get-DoubleCastTime
#Double Cast: 545.2668

Get-DoubleCastTime
#Double Cast: 514.2103

Get-SingleCastTime
#Single Cast: 526.9188




相关问题
Mutually exclusive powershell parameters

SCENARIO I m writing a cmdlet for Powershell 2.0 using Visual Studio 2008 and .NET 3.5 the cmdlet requires 3 arguments. my intended grammar of the cmdlet is something like this: cmdletname [foo|...

Run a program from PowerShell with timeout

I ll write a script that runs a program and wait for it finished. But if the program is not finished within a specified time I want that the program is killed.

How to transpose data in powershell

I have a file that looks like this: a,1 b,2 c,3 a,4 b,5 c,6 (...repeat 1,000s of lines) How can I transpose it into this? a,b,c 1,2,3 4,5,6 Thanks

Powershell v2 remoting and delegation

I have installed Powershell V2 on 2 machines and run Enable-PsRemoting on both of them. Both machines are Win 2003 R2 and are joined to the same active directory domain and I can successfully run ...

PowerShell -match operator and multiple groups

I have the following log entry that I am processing in PowerShell I m trying to extract all the activity names and durations using the -match operator but I am only getting one match group back. I m ...