[英]Fastest way to distinct count a group based on another variable in the same line(distinct count of visitors to a page)
我有一个包含两列的文件; visitorId和pageID。 我想找到的是每个页面的唯一身份/独立访客数。 我在HashTable(dictionary)中使用HashTable来跟踪该特定页面是否已计入该特定访问者。 该文件包含超过10亿行,因此性能非常关键。 除了HashTable中的HashTable之外,是否还有其他数据结构可用来计算不同的访问者?
我必须解决文件上的此问题,因此无法导入数据库。 开发环境是.NET,语言是C#。
您可以在下面找到代码:
Dictionary<int, Dictionary<int, bool>> dicVisitorCount = new Dictionary<int, Dictionary<int, bool>>();
Dictionary<int, int> dicPages = new Dictionary<int, int>();
int million = 1000000;
for (int i = 0; i < 10 * million; i++)
{
pageID = r.Next(1, 100000);
visitorID = r.Next(1, 1000000);
if (!dicPages.ContainsKey(pageID))
{
dicPages.Add(pageID, 1);
Dictionary<int, bool> dicVisitors = new Dictionary<int, bool>();
dicVisitors.Add(visitorID, true);
dicVisitorCount.Add(pageID, dicVisitors);
}
else
{
if (!dicVisitorCount[pageID].ContainsKey(visitorID))
{
dicVisitorCount[pageID].Add(visitorID, true);
dicPages[pageID]++;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.