创建C#中的随机文件 [英] Creating a Random File in C#

查看:100
本文介绍了创建C#中的随机文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个指定大小的文件 - 我不在乎是什么在它的数据,尽管随机的将是很好。目前,我这样做:

I am creating a file of a specified size - I don't care what data is in it, although random would be nice. Currently I am doing this:

        var sizeInMB = 3; // Up to many Gb
        using (FileStream stream = new FileStream(fileName, FileMode.Create))
        {
            using (BinaryWriter writer = new BinaryWriter(stream))
            {
                while (writer.BaseStream.Length <= sizeInMB * 1000000)
                {
                    writer.Write("a"); //This could be random. Also, larger strings improve performance obviously
                }
                writer.Close();
            }
        }

这是效率不高,甚至正确的方式去了解它。任何更高性能的解决方案?

This isn't efficient or even the right way to go about it. Any higher performance solutions?

感谢所有的答案。

跑了2GB的文件下面的方法(时间以毫秒为单位)一些测试:

Ran some tests on the following methods for a 2Gb File (time in ms):

byte[] data = new byte[sizeInMb * 1024 * 1024];
Random rng = new Random();
rng.NextBytes(data);
File.WriteAllBytes(fileName, data);



N / A - 内存不够的异常2GB的文件

N/A - Out of Memory Exception for 2Gb File

byte[] data = new byte[8192];
Random rng = new Random();
using (FileStream stream = File.OpenWrite(fileName))
{
    for (int i = 0; i < sizeInMB * 128; i++)
    {
         rng.NextBytes(data);
         stream.Write(data, 0, data.Length);
    }
}



@ 1000 - 45,868,23283,23346

@1K - 45,868, 23,283, 23,346

@ 128K - 24877,20585,20716

@128K - 24,877, 20,585, 20,716

@的8Kb - 30426,22936,22936

@8Kb - 30,426, 22,936, 22,936

using (var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None))
{
    fs.SetLength(sizeInMB * 1024 * 1024);
}



257,287,3,3,2,3等。

257, 287, 3, 3, 2, 3 etc.

推荐答案

好了,的非常的简单解决方案:

Well, a very simple solution:

byte[] data = new byte[sizeInMb * 1024 * 1024];
Random rng = new Random();
rng.NextBytes(data);
File.WriteAllBytes(fileName, data);

一个稍微更内存高效的版本:)

A slightly more memory efficient version :)

// Note: block size must be a factor of 1MB to avoid rounding errors :)
const int blockSize = 1024 * 8;
const int blocksPerMb = (1024 * 1024) / blockSize;
byte[] data = new byte[blockSize];
Random rng = new Random();
using (FileStream stream = File.OpenWrite(fileName))
{
    // There 
    for (int i = 0; i < sizeInMb * blocksPerMb; i++)
    {
        rng.NextBytes(data);
        stream.Write(data, 0, data.Length);
    }
}



不过,如果你的非常的连续快速创建随机每一次的新实例,你可能会得到重复的数据。请参阅随机性我本文的详细信息 - 你可以用这个的 System.Security.Cryptography.RandomNumberGenerator ...或通过重用随机多次的同一个实例 - 需要提醒的是它不是线程安全的。

However, if you do this several times in very quick succession creating a new instance of Random each time, you may get duplicate data. See my article on randomness for more information - you could avoid this using System.Security.Cryptography.RandomNumberGenerator... or by reusing the same instance of Random multiple times - with the caveat that it's not thread-safe.

这篇关于创建C#中的随机文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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