gpt4 book ai didi

c++ - 为什么我的程序很慢?我怎样才能提高它的效率?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:32:53 27 4
gpt4 key购买 nike

我有一个执行 Block Nested loop join ( link text ) 的程序。基本上它所做的是,它将文件(比如 10GB 文件)中的内容读入 buffer1(比如 400MB),然后将其放入哈希表中。现在将第二个文件(比如 10GB 文件)的内容读入缓冲区 2(比如 100MB)并查看缓冲区 2 中的元素是否存在于哈希中。输出结果无关紧要。我现在只关心程序的效率。在这个程序中,我需要一次从两个文件中读取 8 个字节,所以我使用了 long long int。问题是我的程序效率很低。我怎样才能让它更有效率?

//我使用 g++ -o hash hash.c -std=c++0x

编译
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include <stdint.h>
#include <math.h>
#include <limits.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <unordered_map>
using namespace std;

typedef std::unordered_map<unsigned long long int, unsigned long long int> Mymap;
int main()
{

uint64_t block_size1 = (400*1024*1024)/sizeof(long long int); //block size of Table A - division operator used to make the block size 1 mb - refer line 26,27 malloc statements.
uint64_t block_size2 = (100*1024*1024)/sizeof(long long int); //block size of table B

int i=0,j=0, k=0;
uint64_t x,z,l=0;
unsigned long long int *buffer1 = (unsigned long long int *)malloc(block_size1 * sizeof(long long int));
unsigned long long int *buffer2 = (unsigned long long int *)malloc(block_size2 * sizeof(long long int));

Mymap c1 ; // Hash table
//Mymap::iterator it;

FILE *file1 = fopen64("10G1.bin","rb"); // Input is a binary file of 10 GB
FILE *file2 = fopen64("10G2.bin","rb");

printf("size of buffer1 : %llu \n", block_size1 * sizeof(long long int));
printf("size of buffer2 : %llu \n", block_size2 * sizeof(long long int));


while(!feof(file1))
{
k++;
printf("Iterations completed : %d \n",k);
fread(buffer1, sizeof(long long int), block_size1, file1); // Reading the contents into the memory block from first file

for ( x=0;x< block_size1;x++)
c1.insert(Mymap::value_type(buffer1[x], x)); // inserting values into the hash table

// std::cout << "The size of the hash table is" << c1.size() * sizeof(Mymap::value_type) << "\n" << endl;

/* // display contents of the hash table
for (Mymap::const_iterator it = c1.begin();it != c1.end(); ++it)
std::cout << " [" << it->first << ", " << it->second << "]";
std::cout << std::endl;
*/

while(!feof(file2))
{
i++; // Counting the number of iterations
// printf("%d\n",i);

fread(buffer2, sizeof(long long int), block_size2, file2); // Reading the contents into the memory block from second file

for ( z=0;z< block_size2;z++)
c1.find(buffer2[z]); // finding the element in hash table

// if((c1.find(buffer2[z]) != c1.end()) == true) //To check the correctness of the code
// l++;
// printf("The number of elements equal are : %llu\n",l); // If input files have exactly same contents "l" should print out the block_size2
// l=0;
}
rewind(file2);
c1.clear(); //clear the contents of the hash table
}

free(buffer1);
free(buffer2);
fclose(file1);
fclose(file2);
}

更新:

是否可以使用 C++ 流读取器直接从文件中读取一个 block (比如 400 MB)并直接将其放入哈希表中?我认为这可以进一步减少开销。

最佳答案

如果您正在使用 fread,请尝试使用 setvbuf() .标准库文件 I/O 调用使用的默认缓冲区很小(通常为 4kB 量级)。当快速处理大量数据时,您将受到 I/O 限制,获取许多小缓冲区数据的开销可能成为一个重要的瓶颈。将其设置为更大的大小(例如 64kB 或 256kB),您可以减少开销并且可能会看到显着的改进 - 尝试一些值以查看您获得最佳 yield 的地方,因为您将获得递减的 yield .

关于c++ - 为什么我的程序很慢?我怎样才能提高它的效率?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3867947/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com