我有一个读取 2 个输入文件的程序。第一个文件包含一些随机词,这些词被放入 BST 和 AVL 树中。然后程序查找第二个读取文件中列出的单词,并说明它们是否存在于树中,然后将收集到的信息写入一个输出文件。在执行此操作时,程序会打印出查找某个项目所花费的时间。但是,该程序似乎没有测量花费的时间。
BST* b = new BST();
AVLTree* t = new AVLTree();
string s;
ifstream in;
in.open(argv[1]);
while(!in.eof())
{
in >> s;
b->insert(s);
t->insert(s);
}
ifstream q;
q.open(argv[2]);
ofstream out;
out.open(argv[3]);
int bstItem = 0;
int avlItem = 0;
float diff1 = 0;
float diff2 = 0;
clock_t t1, t1e, t2, t2e;
while(!q.eof())
{
q >> s;
t1 = clock();
bstItem = b->findItem(s);
t1e = clock();
diff1 = (float)(t1e - t1)/CLOCKS_PER_SEC;
t2 = clock();
avlItem = t->findItem(s);
t2e = clock();
diff2 = (float)(t2e - t2)/CLOCKS_PER_SEC;
if(avlItem == 0 && bstItem == 0)
cout << "Query " << s << " not found in " << diff1 << " microseconds in BST, " << diff2 << " microseconds in AVL" << endl;
else
cout << "Query " << s << " found in " << diff1 << " microseconds in BST, " << diff2 << " microseconds in AVL" << endl;
out << bstItem << " " << avlItem << " " << s << "\n";
}
我在进入 while 之前和完成之后得到的 clock() 值完全相同。所以看起来程序甚至根本没有运行while循环,所以它打印0。我知道情况并非如此,因为程序完成它应该需要大约10秒。此外,输出文件包含正确的结果,因此存在错误的 findItem() 函数的可能性也不正确。
我在 Stack Overflow 上做了一些研究,发现很多人都遇到了和我一样的问题。然而,我读过的答案都没有解决它。