.net 在堆栈溢出时要做到这一点,否则很快就会变成一堆垃圾。回答者的责任不仅仅是看问题的标题。这个“答案”实质上是将一段工作代码从问题转换成方法语法。OP并没有要求这样做,所以答案只不过是无用的杂乱无章的东西,它会跟踪未来的读者。作者应该删除它,但他没有,因为他将
在堆栈溢出时要做到这一点,否则很快就会变成一堆垃圾。回答者的责任不仅仅是看问题的标题。这个“答案”实质上是将一段工作代码从问题转换成方法语法。OP并没有要求这样做,所以答案只不过是无用的杂乱无章的东西,它会跟踪未来的读者。作者应该删除它,但他没有,因为他将失去8点声誉。.net 在堆栈溢出时要做到这一点,否则很快就会变成一堆垃圾。回答者的责任不仅仅是看问题的标题。这个“答案”实质上是将一段工作代码从问题转换成方法语法。OP并没有要求这样做,所以答案只不过是无用的杂乱无章的东西,它会跟踪未来的读者。作者应该删除它,但他没有,因为他将,.net,linq,performance,foreach,.net,Linq,Performance,Foreach,在堆栈溢出时要做到这一点,否则很快就会变成一堆垃圾。回答者的责任不仅仅是看问题的标题。这个“答案”实质上是将一段工作代码从问题转换成方法语法。OP并没有要求这样做,所以答案只不过是无用的杂乱无章的东西,它会跟踪未来的读者。作者应该删除它,但他没有,因为他将失去8点声誉。 foreach (var word in allWords) { if (wordCount.
foreach (var word in allWords)
{
if (wordCount.ContainsKey(word))
wordCount[word]++;
else
wordCount.Add(word, 1);
}
var wordCountLINQ = (from word in allWordsLINQ
group word by word
into groups
select groups).ToDictionary(g => g.Key, g => g.Count());
public static void TestCode()
{
//File can be downloaded from http://norvig.com/big.txt and consists of about a million words.
const string fileName = @"path_to_file";
var allWords = from Match m in Regex.Matches(File.ReadAllText(fileName).ToLower(), "[a-z]+", RegexOptions.Compiled)
select m.Value;
var wordCount = new Dictionary<string, int>();
var timer = new Stopwatch();
timer.Start();
foreach (var word in allWords)
{
if (wordCount.ContainsKey(word))
wordCount[word]++;
else
wordCount.Add(word, 1);
}
timer.Stop();
Console.WriteLine("foreach loop took {0:0.00} ms ({1:0.00} secs)\n",
timer.ElapsedMilliseconds, timer.ElapsedMilliseconds / 1000.0);
//Make LINQ use a different Enumerable (with the exactly the same values),
//if you don't it suddenly becomes way faster, which I assmume is a caching thing??
var allWordsLINQ = from Match m in Regex.Matches(File.ReadAllText(fileName).ToLower(), "[a-z]+", RegexOptions.Compiled)
select m.Value;
timer.Reset();
timer.Start();
var wordCountLINQ = (from word in allWordsLINQ
group word by word
into groups
select groups).ToDictionary(g => g.Key, g => g.Count());
timer.Stop();
Console.WriteLine("LINQ took {0:0.00} ms ({1:0.00} secs)\n",
timer.ElapsedMilliseconds, timer.ElapsedMilliseconds / 1000.0);
}
Dictionary<string, int> wordCountLINQ = allWordsLINQ.GroupBy<string, string>(delegate (string word) {
return word;
}).Select<IGrouping<string, string>, IGrouping<string, string>>(delegate (IGrouping<string, string> groups) {
return groups;
}).ToDictionary<IGrouping<string, string>, string, int>(delegate (IGrouping<string, string> g) {
return g.Key;
}, delegate (IGrouping<string, string> g) {
return g.Count<string>();
});
from word in allWordsLINQ
group word by word into groups
select new { Word = groups.Key, Count = groups.Count() }
(from word in allWordsLINQ
group word by word into groups
select new { Word = groups.Key, Count = groups.Count() })
.ToDictionary(g => g.Word, g => g.Count);
var words = unitOfWork.DepartmentRepository.Get()
.GroupBy(a=>a.word).Select(s => new
{
Word = s.Key,
Count = s.Count()
}).ToDictionary(d=>d.Word, d=>d.Count);
var wordCountLINQ = allWordsLINQ.Aggregate(new Dictionary<string, int>(), (wcld, w) => { wcld[w] = (wcld.ContainsKey(w) ? wcld[w] : 0) + 1; return wcld; })