提高Powershell性能以生成随机文件 [英] Improve Powershell Performance to Generate a Random File

查看:88
本文介绍了提高Powershell性能以生成随机文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让Powershell用户创建一个随机文本文件以用于基本系统测试(上传,下载,校验和等). 我已使用以下文章并提供了自己的代码段来创建随机文本文件,但性能却很糟糕.

I'd like to user Powershell to create a random text file for use in basic system testing (upload, download, checksum, etc). I've used the following articles and come up with my own code snippet to create a random text file but the performance is terrible.

  • Generating Random Files in Windows (stackoverflow.com)
  • PowerShell – Creating Dummy files (verboon.info)
  • Create large files with Powershell (chris-nullpayload.rhcloud.com based on verboon code above)

这是我的代码示例,在现代Windows 7 Dell笔记本电脑上,该代码示例大约需要227秒才能生成1MB的随机文本文件.运行时间是使用Measure-Command cmdlet确定的.我在不同的系统负载下重复了几次测试,并获得了相似的长时间运行结果.

Here is my code sample that takes approximately 227 seconds to generate a 1MB random text file on a modern Windows 7 Dell laptop. Run time was determined using the Measure-Command cmdlet. I repeated the test several times during different system load with similar long runtime results.

# select characters from 0-9, A-Z, and a-z
$chars = [char[]] ([char]'0'..[char]'9' + [char]'A'..[char]'Z' + [char]'a'..[char]'z')
# write file using 128 byte lines each with 126 random characters
1..(1mb/128) | %{-join (1..126 | %{get-random -InputObject $chars }) } `
  | out-file test.txt -Encoding ASCII

我正在寻找讨论为什么此代码的性能较差的答案,以及针对我可以做出的简单更改以改善运行时间的建议,以生成类似的随机文本文件( 126个随机字母数字字符的ASCII文本行-带"\ r \ n" EOL的128个字节,输出文件为偶数个兆字节,例如上面的1MB示例).我希望将文件输出分段(每次一行或多行),这样我们就不再需要存储在内存中的输出文件大小的字符串了.

I am looking for answers that discuss why this code has poor performance and suggestions for simple changes I can make to improve the runtime for generating a similar random text file (ASCII text lines of 126 random alphanumeric characters - 128 bytes with "\r\n" EOL, output file an even number of megabytes such as the above 1MB sample). I would like file output to be written in pieces (one or more lines at a time) so that we never need a string the size of the output file stored in memory.

推荐答案

同意@dugas瓶颈正在为每个字符调用Get-Random.

Agree with @dugas that the bottleneck is calling Get-Random for every character.

如果增加字符数组集并使用Get-Random的-count属性,则应该能够实现几乎相同的随机性.

You should be able to achieve nearly the same randomness if you increase your character array set, and use the -count property of Get-Random.

如果您使用的是V4,则.foreach方法要比foreach-object快得多.

If you have V4, the .foreach method is considerably faster than foreach-object.

也将Out-File换为Add-Content,这也应该有所帮助.

Also traded Out-File for Add-Content, which should also help.

# select characters from 0-9, A-Z, and a-z
$chars = [char[]] ([char]'0'..[char]'9' + [char]'A'..[char]'Z' + [char]'a'..[char]'z')
$chars = $chars * 126
# write file using 128 byte lines each with 126 random characters
(1..(1mb/128)).foreach({-join (Get-Random $chars -Count 126) | add-content testfile.txt }) 

这在我的系统上完成了大约32秒.

That finished in about 32 seconds on my system.

使用生成的测试文件设置内容vs外文件:

Set-Content vs Out-File, using the generated test file:

$x = Get-Content testfile.txt

(Measure-Command {$x | out-file testfile1.txt}).totalmilliseconds
(Measure-Command {$x | Set-Content testfile1.txt}).totalmilliseconds

504.0069
159.0842

这篇关于提高Powershell性能以生成随机文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆