无法创建巨大的数组 [英] Can't create huge arrays

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

问题描述

像许多其他程序员一样,我进入了素数,和他们中的许多人一样,我喜欢挑战,所以我不是在寻找像 Atkin 做这件事的速度比你哥们快那样的评论,而只是对我的问题的一个解决方案 - 或者至少是一个提示.

我需要创建数组(如大小>int.MaxValue).所以我去了很多网页,找到了 gcAllowVeryLargeObjects 元素一个.我以为我得救了,在我的 App.config 中添加以下魔法:

<预><代码><配置><运行时><gcAllowVeryLargeObjects enabled="true"/></运行时></配置>

但它没有起作用.这是我使用的代码:

void go(object sender, EventArgs eventArgs){t.停止();ulong maxprime = 10;秒表 秒表 = new Stopwatch();字符串 s = String.Empty;而 (maxprime < ulong.MaxValue){秒表.重启();richTextBox2.Text += Environment.NewLine + ("Max \t= " + maxprime.ToString("N0"));尝试{richTextBox2.Text += Environment.NewLine + ("Count \t= " + GetAllPrimesLessThan(maxprime).Count);richTextBox2.Text += Environment.NewLine + ("Time \t= " + 秒表.Elapsed);richTextBox2.Text += Environment.NewLine + ("--------------------------------");最大素数 *= 10;richTextBox2.Refresh();}捕获(异常异常){s = exception.Message + "; 分配大小:" + (maxprime + 1).ToString("N0");休息;}}if (!string.IsNullOrEmpty(s)){richTextBox2.Text += Environment.NewLine + s;}richTextBox2.Text += Environment.NewLine + ("Done.");}私有静态列表GetAllPrimesLessThan(ulong maxPrime){var primes = new List() { 2 };var maxSquareRoot = Math.Sqrt(maxPrime);var 淘汰 = new bool[maxPrime + 1];for (ulong i = 3; i <= maxPrime; i += 2){如果(!消除[i]){素数.Add(i);如果 (i < maxSquareRoot){for (ulong j = i * i; j <= maxPrime; j += 2 * i){消除[j] = 真;}}}}返回质数;}

输出:

<代码>[...]最大值 = 1 000 000 000计数 = 50847534时间 = 00:00:15.3355367--------------------------------最大值 = 10 000 000 000数组尺寸超出了支持的范围.分配大小:10 000 000 001完毕.

我怎样才能摆脱这个错误?

<小时>

仅供参考:我有

  • 16GB 内存;
  • 32GB 内存映射(/分页?)在 SSD 上;
  • 启用 64 位

解决方案

来自您的链接:

<块引用>

在应用程序配置文件中使用此元素可启用大小超过 2 GB 的数组,但不会更改对象大小或数组大小的其他限制:

字节数组和单字节结构数组的任何单个维度的最大索引为 2,147,483,591 (0x7FFFFFC7),其他类型为 2,146,435,071 (0X7FEFFFFF).

另见 64 位 Windows 上 .NET 中数组的最大长度是多少:

<块引用>

理论上,一个数组最多可以有 2,147,483,647 个元素,因为它使用 int 进行索引.

Like many other programmers, I went into primes, and as many of them, what I like is the challenge, so I'm not looking for comment like Atkin did this faster than you dude, but just a solution - or at least an hint - to my issue.

I need to create big arrays (like size > int.MaxValue). So I went to a lot of web pages and found the gcAllowVeryLargeObjects Element one. I thought I was saved, add the following magic to my App.config:

<configuration>
  <runtime>
    <gcAllowVeryLargeObjects enabled="true" />
  </runtime>
</configuration>

But it didn't worked. Here's the code I use :

void go(object sender, EventArgs eventArgs)
{
    t.Stop();
    ulong maxprime = 10;
    Stopwatch stopwatch = new Stopwatch();
    string s = String.Empty;
    while (maxprime < ulong.MaxValue)
    {
        stopwatch.Restart();
        richTextBox2.Text += Environment.NewLine + ("Max \t= " + maxprime.ToString("N0"));
        try
        {
            richTextBox2.Text += Environment.NewLine + ("Count \t= " + GetAllPrimesLessThan(maxprime).Count);
            richTextBox2.Text += Environment.NewLine + ("Time \t= " + stopwatch.Elapsed);
            richTextBox2.Text += Environment.NewLine + ("--------------------------------");
            maxprime *= 10;
            richTextBox2.Refresh();
        }
        catch (Exception exception)
        {
            s = exception.Message + "; Allocation size: " + (maxprime + 1).ToString("N0");
            break;
        }

    }
    if (!string.IsNullOrEmpty(s))
    {
        richTextBox2.Text += Environment.NewLine + s;
    }
    richTextBox2.Text += Environment.NewLine + ("Done.");
}

private static List<ulong> GetAllPrimesLessThan(ulong maxPrime)
{
    var primes = new List<ulong>() { 2 };
    var maxSquareRoot = Math.Sqrt(maxPrime);
    var eliminated = new bool[maxPrime + 1];

    for (ulong i = 3; i <= maxPrime; i += 2)
    {
        if (!eliminated[i])
        {
            primes.Add(i);
            if (i < maxSquareRoot)
            {
                for (ulong j = i * i; j <= maxPrime; j += 2 * i)
                {
                    eliminated[j] = true;
                }
            }
        }
    }
    return primes;
}

Which output this:

[...]
Max     = 1 000 000 000
Count   = 50847534
Time    = 00:00:15.3355367
--------------------------------
Max     = 10 000 000 000
Array dimensions exceeded supported range.; Allocation size: 10 000 000 001
Done.

How can I get rid of this error?


FYI: I've got

  • 16GB ram;
  • 32GB memory mapped(/paged?) on SSD;
  • 64bits enabled

解决方案

From your link:

Using this element in your application configuration file enables arrays that are larger than 2 GB in size, but does not change other limits on object size or array size:

The maximum index in any single dimension is 2,147,483,591 (0x7FFFFFC7) for byte arrays and arrays of single-byte structures, and 2,146,435,071 (0X7FEFFFFF) for other types.

See also What is the maximum length of an array in .NET on 64-bit Windows:

An array could theoretically have at most 2,147,483,647 elements, since it uses an int for indexing.

这篇关于无法创建巨大的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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