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" です。

4

1 に答える 1

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は同じです。

于 2012-10-31T20:20:32.310 に答える