2012-10-31 104 views
7

示例代码:

# Step 1 
$start = get-date 
for($i=1; $i -le 1000000; $i++){} 
$end = get-date 
($end-$start).TotalMilliseconds 
Remove-Variable i 

# Step 2 
$start = get-date 
for([int]$i=1; $i -le 1000000; $i++){} 
$end = get-date 
($end-$start).TotalMilliseconds 
Remove-Variable i 

# Step 3 
$start = get-date 
for([int64]$i=1; $i -le 1000000; $i++){} 
$end = get-date 
($end-$start).TotalMilliseconds 
Remove-Variable i 

# Step 4 
$start = get-date 
for([float]$i=1; $i -le 1000000; $i++){} 
$end = get-date 
($end-$start).TotalMilliseconds 
Remove-Variable i 

# Step 5 
$start = get-date 
for([double]$i=1; $i -le 1000000; $i++){} 
$end = get-date 
($end-$start).TotalMilliseconds 
Remove-Variable i 

# Step 6 
$start = get-date 
for($i=1; $i -le 1000000; $i++){} 
$end = get-date 
($end-$start).TotalMilliseconds 
Remove-Variable i 

结果:

1845.1056
3160.1808
5029.2877
5521.3158
4504.2576
1804.1032

没有关于步骤2-6之间的差异问题。但1和2和6之间的区别是不可理解的:$ i在这些情况下具有类型“System.Int32”。

+0

我看到的结果与您大致相同。 –

+3

测量执行时间的方式本质上没有什么错,但是为了将来的测试,您可能需要在提示符下检查:get-help测量命令 – EBGreen

+2

有趣的是,如果您执行'$ i = new -object -type Int64'或'$ i = new-object -type Int32'然后'for($ i = 1; $ i -le 1000000; $ i ++){}',你会得到类似的结果整数)。在那个演员操作符或许... – zdan

回答

4

如果你想步骤1和步骤之间的差异的一个很好的解释2只是尝试在命令提示符下:

Remove-Variable i 
Trace-Command -Name TypeConversion -Expression {for($i=1; $i -le 1000000; $i++){}} -PSHost 

然后:

Remove-Variable i 
Trace-Command -Name TypeConversion -Expression {for([int]$i=1; $i -le 1000000; $i++){}} -PSHost 

此确认@zdan假设的差异是在每个循环中完成的演员。步骤1和6是相同的。

+0

+1在我提醒你我的答案之前,我写了同样的答案 –

相关问题