示例代码:
# 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“类型。
发布于 2012-11-01 04:20:32
如果您想很好地解释步骤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是相同的。
https://stackoverflow.com/questions/13163635
复制相似问题