【问题标题】:Program will not output data to console when using a data input size greater than 30 million使用大于 3000 万的数据输入大小时,程序不会将数据输出到控制台
【发布时间】:2015-09-09 00:11:12
【问题描述】:

我正在尝试制作一个程序,该程序最终将通过使用二叉搜索树和向量来显示大数据输入的运行时差异。但在此之前,我正在测试插入和搜索功能是否正常工作。这似乎很好,但每当我将SIZE 分配为 3000 万或更多时,大约 10-20 秒后,它只会显示Press any key to continue... 而没有输出。但是,如果我将SIZE 分配为等于或小于 2000 万,它将按照我的编程输出搜索结果。那么您认为是什么导致了这个问题呢?

一些旁注:

我将一个唯一的(不重复的)随机生成的值存储到树和向量中。所以最后,树和向量都将具有完全相同的值。当程序运行搜索部分时,如果在 BST 中找到了一个值,那么它也应该在向量中找到。到目前为止,这在使用 2000 万个或更少的值时没有问题。

另外,我使用randValue = rand() * rand(); 来生成随机值,因为我知道 rand() 的最大值是 32767。因此,将它自身相乘将保证从 0 到 1,073,741,824 的数字范围。我知道我使用的插入和搜索方法效率低下,因为我确保没有重复,但现在这不是我关心的问题。这只是为了我自己的练习。

为了简单起见,我只发布我的 main.cpp。如果您认为问题出在我的其他文件之一,我将发布其余文件。

这是我的 main.cpp:

#include <iostream>
#include <time.h>
#include <vector>
#include "BSTTemplate.h"
#include "functions.h"

using namespace std;

int main()
{
    const long long SIZE = 30000000;
    vector<long long> vector1(SIZE);
    long long randNum;
    binarySearchTree<long long> bst1;
    srand(time(NULL));

    //inserts data into BST and into the vector AND makes sure there are no duplicates
    for(long long i = 0; i < SIZE; i++)
    {
        randNum = randLLNum();
        bst1.insert(randNum);
        if(bst1.numDups == 1)//if the random number generated is duplicated, don't count it and redo that iteration
        {
            i--;
            bst1.numDups = 0;
            continue;
        }
        vector1[i] = randNum;
    }

    //search for a random value in both the BST and the vector
    for(int i = 0; i < 5; i++)
    {
        randNum = randLLNum();
        cout << endl << "The random number chosen is: " << randNum << endl << endl;

        //searching with BST
        cout << "Searching for " << randNum << " in BST..." << endl;
        if(bst1.search(randNum))
            cout << randNum << " = found" << endl;
        else
            cout << randNum << " = not found" << endl;

        //searching with linear search using vectors
        cout << endl << "Searching for " << randNum << " in vector..." << endl;
        if(containsInVector(vector1, SIZE, randNum))
            cout << randNum << " = found" << endl;
        else
            cout << randNum << " = not found" << endl;
    }

    cout << endl;

    return 0;
}

【问题讨论】:

  • 我猜这里的问题是控制台本身。尝试运行yourprogram &gt; testfile.txt 将程序的输出重定向到文件。
  • 您的程序可能内存不足并崩溃。尝试在调试器下运行它,或者从终端窗口手动运行它,看看它是否以不同的方式报告问题。你也可以添加一个trycatch 块,看看你是否收到std::bad_alloc。回复rand,检查RAND_MAX - 标准只要求它 >= 32767,但在许多实现中它会更多 - 例如2^31-1。无论如何,rand() * rand(); 错过了该范围内的许多潜在值(例如所有素数):如果您愿意,可以添加 rand()
  • @TonyD 我使用“开始调试”(而不是在不调试的情况下开始)运行程序,我收到了您正在谈论的std::bad_alloc 消息。有解决办法吗?
  • 选项包括:编译 64 位(如果您还没有编译 - 可能会变得更好或更糟,具体取决于 RAM 或地址空间是否存在问题)、购买更多内存、调整操作系统的交换内存设置(让它使用更多磁盘),设计一个内存效率更高的树(但充其量你可能只会得到一个数量级的改进,也许更少,它可能会影响其他事情,比如性能特征),重新设计你的树它手动将数据保存到磁盘并读回(例如使用 LRU)。
  • 这里是一个操作方法:msdn.microsoft.com/en-us/library/9yb4317s.aspx

标签: c++ vector console-application binary-search-tree dynamic-memory-allocation


【解决方案1】:

(应 OP 的要求将评论重新发布为答案)

选项包括:编译 64 位(如果您尚未编译 - 可能会变得更好或更糟,具体取决于 RAM 或地址空间是否存在问题)、购买更多内存、调整操作系统的交换内存设置(让它使用更多磁盘),设计一个内存效率更高的树(但充其量你可能只会获得一个数量级的改进,可能更少,并且可能会影响其他方面,例如性能特征),重新设计你的树,以便手动保存数据到磁盘并将其读回(例如使用 LRU)。

这是在 VC++ 上编译 64 位的方法:msdn.microsoft.com/en-us/library/9yb4317s.aspx

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-12
    • 2011-08-20
    相关资源
    最近更新 更多