Итоги тестов, вот такого кода:
тестовый код
Код:
$file = 'out1500000.txt'
'------'
'1 - gc $file'
(measure-command{
1..3|%{$arr = $null}{$arr = gc $file}
write-host $($arr.count) -for red
}).totalseconds
'------'
'2 - gc $file -ReadCount 10000'
(measure-command{
1..3|%{$arr = $null}{$arr = gc $file -ReadCount 10000}
write-host $($arr.count) -for red
}).totalseconds
'------'
'3 - (gc $file -raw) -split "`n"'
(measure-command{
1..3|%{$arr = $null}{$arr = (gc $file -raw) -split "`n"}
write-host $($arr.count) -for red
}).totalseconds
'------'
'4 - switch -file $file {default {$psitem}}'
(measure-command{
1..3|%{$arr = $null}{$arr = switch -file $file {default {$psitem}}}
write-host $($arr.count) -for red
}).totalseconds
'------'
'5 - [io.file]::readalltext($file) -split "`n"'
(measure-command{
1..3|%{$arr = $null}{$arr = [io.file]::readalltext($file) -split "`n"}
write-host $($arr.count) -for red
}).totalseconds
'------'
'6 - [io.file]::readalllines($file)'
(measure-command{
1..3|%{$arr = $null}{$arr = [io.file]::readalllines($file)}
write-host $($arr.count) -for red
}).totalseconds
'------'
'7 - [io.file]::readlines($file) -split "`n"'
(measure-command{
1..3|%{$arr = $null}{$arr = [io.file]::readlines($file) -split "`n"}
write-host $($arr.count) -for red
}).totalseconds
'------'
'8 - [io.file]::opentext($file).readtoend() -split "`n"'
(measure-command{
1..3|%{$arr = $null}{$arr = [io.file]::opentext($file).readtoend() -split "`n"}
write-host $($arr.count) -for red
}).totalseconds
'------'
'9 - $reader.readtoend() -split "`n"'
(measure-command{
1..3|%{$arr = $null}{
$reader = [io.streamreader]::new($file)
$arr = $reader.readtoend() -split "`n"
$reader.close()
}
write-host $($arr.count) -for red
}).totalseconds
- чтобы получить более-менее корректные сравнения, цель работы каждого блока была такой:
Считать из файла 1500000 строк и получить массив такого же количества строк.
PS 7.3.6
Цитата:
------
1 - gc $file
1500000
135,3993815
------
2 - gc $file -ReadCount 10000
150
4,5613858
------
3 - (gc $file -raw) -split "`n"
1500001
3,8927897
------
4 - switch -file $file {default {$psitem}}
1500000
1,8711774
------
5 - [io.file]::readalltext($file) -split "`n"
1500001
1,4145144
------
6 - [io.file]::readalllines($file)
1500000
0,8467002
------
7 - [io.file]::readlines($file) -split "`n"
1500000
2,5423216
------
8 - [io.file]::opentext($file).readtoend() -split "`n"
1500001
1,4478526
------
9 - $reader.readtoend() -split "`n"
1500001
1,7025707
|
PS 5.1
Цитата:
------
1 - gc $file
1500000
71,0913368
------
2 - gc $file -ReadCount 10000
150
2,0562238
------
3 - (gc $file -raw) -split "`n"
1500001
5,4825919
------
4 - switch -file $file {default {$psitem}}
1500000
1,6654813
------
5 - [io.file]::readalltext($file) -split "`n"
1500001
3,9344098
------
6 - [io.file]::readalllines($file)
1500000
0,5840069
------
7 - [io.file]::readlines($file) -split "`n"
1500000
2,4861966
------
8 - [io.file]::opentext($file).readtoend() -split "`n"
1500001
3,6093053
------
9 - $reader.readtoend() -split "`n"
1500001
3,2849391
|
- понятно, что безоговорочным победителем скорочтения массива строк, становится [io.file]::readalllines($file), ибо никаких дополнительных разбивок не требует...
|