PowerShell随机文件生成器太慢 [英] PowerShell random file generator too slow
本文介绍了PowerShell随机文件生成器太慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尝试使用以下PowerShell脚本在Windows中生成随机文件:
Trying to generate random files in Windows with this PowerShell script:
param([string]$path = "C:\temp", [int]$size = "1024", [long]$number= "1000")
Write-Host "Path: $path"
Write-Host "Size of file: $size"
Write-Host "Number of files: $number"
Write-Host "Started at:"
Get-Date -Format HH:mm:ss
md -Force $path |out-null
$script:StartTime = $(Get-Date)
for($i=0; $i -le $number - 1 ; $i++){
[Byte[]]$out=@(); 0..($size-1) | %{$out += Get-Random -Minimum 0 -Maximum 255};
[System.IO.File]::WriteAll("$path\$i.bin",$out)
if (-Not ($i % 100) -and ($i -ne 0 )) {
Write-Host "$i were created"
$elapsedTime = $(Get-Date) - $script:StartTime
$script:StartTime = $(Get-Date)
Write-Host "$elapsedTime"
}
}
当我要将文件大小从1KB增加到1MB时,即使一个文件也要花费很长时间,但是我需要成千上万个文件.有办法解决这个瓶颈吗?
When I want to increase filesize from 1KB to 1MB, it take a long time for even one file, but I need thousands of those. Is there way to solve this bottleneck?
推荐答案
已针对Windows为 dd解决了问题.
Solved with dd for windows.
#default values
param([string]$path = "C:\temp", [int]$size = "1048576", [long]$number= "10000")
Write-Host "Path: $path"
Write-Host "Size of file: $size"
Write-Host "Number of files: $number"
##dd.exe should be in same dir
$path_to_dd = Get-Location
Write-Host "Started at:"
Get-Date -Format HH:mm:ss
md -Force $path |out-null
$script:StartTime = $(Get-Date)
for($i=0; $i -le $number - 1 ; $i++){
#genarate files
(& $path_to_dd\dd.exe if=/dev/random of=$path\$i.bin bs=$size count=1) 2>&1 | Out-Null
#periodical report to stdout with timestamp every 1k files
if (-Not ($i % 1000) -and ($i -ne 0 )) {
Write-Host "$i were created"
$elapsedTime = $(Get-Date) - $script:StartTime
$script:StartTime = $(Get-Date)
Write-Host "$elapsedTime"
}
}
Write-Host "Finished at:"
Get-Date -Format HH:mm:ss
这篇关于PowerShell随机文件生成器太慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文