[英]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.