计算大数据流中每个元素的出现次数
Count the occurrence of each element in large data stream
我有一个模拟,有N个粒子,在T个时间步长上运行。在每个时间步,每个粒子计算一些关于自己和附近(半径内)其他粒子的数据,这些数据被打包成一个4-22字节长的c字符串(取决于附近有多少粒子)。我称之为状态字符串。
我需要计算每个状态字符串出现的次数,以形成直方图。我试过使用Google的Sparse Hash Map,但是内存开销太大了。
我一直在运行一些简化的测试(附)超过100,000个时间步,500个粒子。这导致在50mil个可能的状态字符串中只有超过18mil个唯一的状态字符串,这与需要完成的实际工作是一致的。
它最终为每个唯一条目的char*和int以及实际状态字符串本身使用了323 MB的空间。然而,任务管理器报告使用了870M。这是547M的开销,或约251.87比特/条,远远超过谷歌广告的4-5比特。
所以我想我一定是做错了什么。但后来我找到了这个网站,它显示了类似的结果,然而,我不确定他的图表是否只显示了哈希表的大小,还是也包括了实际数据的大小。此外,他的代码没有释放任何插入到已经存在的hashmap中的字符串(这意味着如果他的图表包含实际数据的大小,它将结束)。
以下是显示输出问题的一些代码:#include <google/sparse_hash_map>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
//String equality
struct eqstrc
{
bool operator()(const char* s1, const char* s2) const
{
return (s1 == s2) || (s1 && s2 && !strcmp(s1,s2));
}
};
//Hashing function
template <class T>
class fnv1Hash
{
public:
size_t operator()(const T& c) const {
unsigned int hash = 2166136261;
const unsigned char *key = (const unsigned char*)(c);
size_t L = strlen((const char*)c);
size_t i = 0;
for(const unsigned char *s = key; i < L; ++s, ++i)
hash = (16777619 * hash) ^ (*s);
return (size_t)hash;
}
};
//Function to form new string
char * new_string_from_integer(int num)
{
int ndigits = num == 0 ? 1 : (int)log10((float)num) + 1;
char * str = (char *)malloc(ndigits + 1);
sprintf(str, "%d", num);
return str;
}
typedef google::sparse_hash_map<const char*, int, fnv1Hash<const char*>, eqstrc> HashCharMap;
int main()
{
HashCharMap hashMapChar;
int N = 500;
int T = 100000;
//Fill hash table with strings
for(int k = 0; k < T; ++k)
{
for(int i = 0; i < N; ++i)
{
char * newString = new_string_from_integer(i*k);
std::pair<HashCharMap::iterator, bool> res = hashMapChar.insert(HashCharMap::value_type(newString, HashCharMap::data_type()));
(res.first)->second++;
if(res.second == false) //If the string already in hash map, don't need this memory
free(newString);
}
}
//Count memory used by key
size_t dataCount = 0;
for(HashCharMap::iterator hashCharItr = hashMapChar.begin(); hashCharItr != hashMapChar.end(); ++hashCharItr)
{
dataCount += sizeof(char*) + sizeof(unsigned int); //Size of data to store entries
dataCount += (((strlen(hashCharItr->first) + 1) + 3) & ~0x03); //Size of entries, padded to 4 byte boundaries
}
printf("Hash Map Size: %lun", (unsigned long)hashMapChar.size());
printf("Bytes written: %lun", (unsigned long)dataCount);
system("pause");
}
输出Hash Map Size: 18218975
Bytes written: 339018772
Peak Working Set (Reported by TaskManager): 891,228 K
Overhead: 560,155 K, or 251.87 bits/entry
我已经尝试了谷歌稀疏散列图v1.10和v2.0.2。
我在使用哈希映射时做错了什么吗?或者有更好的方法来解决这个问题,因为对于这些字符串,我几乎可以只存储字符串列表,排序,然后计算连续条目。
谢谢你的帮助
<标题>编辑因为我被问到,这里是实际数据的格式:每个组件是2字节,并分为两个子部分。12位和4位
- 前两个字节(短):[当前粒子id(12位)|角度当前粒子(4位)]
- 秒短:[交互次数]粒子(12比特)(N) |当前粒子的前一个角度(4比特)]
- 对于下一个N短线:[粒子i的id(12比特)|粒子i的前一个角度(4比特)]
角度被近似(除以16),以4位存储。
这有点啰嗦,所以我写一个例子:
0x120A 0x001B 0x136F
=粒子288 (0x120
),夹角为10 (0xA
)。前一时间步长角为11 (0xB
)。与1 (0x001
)其他粒子相互作用。另一个粒子是粒子310 (0x136
),在上一个时间步长角为15 ((0xF
)。
粒子与0到9个其他粒子相互作用,因此我上面提到的4-22字节(尽管很少,可以与多达12个或更多的其他粒子相互作用)。没有限制。如果所有500个粒子都在半径内,那么字符串将是1004字节长)
附加信息:在我的实际代码中,哈希函数和比较函数使用存储在第二短的最高12位中的大小来进行处理,因为非终端0x000s可能出现在我的状态字符串中。
标题>这些图来自Linux上的gcc实验。分配4-22字节的短块,长度为1 - 12需要16字节,长度为13 - 20需要24字节,其余部分需要32字节。
这意味着你对18218975个字符串("0".."50000000")的实验在堆上需要291503600字节,它们的长度之和(加上末尾的0)为156681483。
因此,由于malloc,您有135MB的开销。
(这个峰值工作集大小是一个可靠的数字吗?)
- 使来自线程的数据流对所有其他线程都可读
- 什么是 DirectX 9 中的数据流?
- 将数据流到字节数组中
- 使用 Python 连接到数据流
- 向量元素数据损坏了Find()操作
- 使用 N-API 将数据流式传输到 Node.js C++ 插件中
- 是否存在与将数据流式传输到 c++ 异常类相关的任何危险
- 如何将数据流添加到具有FFmpeg和C / C++的MXF(使用mpeg2video)文件中
- C 中的数据流
- 从QTcpSocket上的数据流中连续运行复杂算法的最佳Qt线程解决方案是什么
- 将数据流式传输到 protobuf 解析器 c++
- 用于大型数据流的通货紧缩压缩算法
- 为什么我的数据流在20或30行后开始重复
- 使用QtMultedia播放自定义avi数据流
- 数据流 Qt 到标准 c++ 代码
- 在数据流处理程序中获取segfault
- 如果缓冲区由数据流填充,则 C++ boost async_read_some被缓冲区阻止
- Mac 上高带宽数据流最有效的进程间通信方法是什么?
- 如何将数据流从 std::cout 发送到 gnuplot
- 计算大数据流中每个元素的出现次数