好的,所以我有一个 C# 控制台应用程序,它应该读取一个 .txt 文件……并计算不同的单词……它可以工作……但是我用 100MB 读取文件中每个不同单词的文件文件它持续了好几天。我想要一种方法来读取文件一次并计算所有不同的单词。到目前为止,这是一些应用程序:
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Diagnostics;
using System.Data;
using System.IO.MemoryMappedFiles;
namespace CompressionApp
{
class Program
{
static void Main(string[] args)
{
//read all text
string FilePath = (@"D:\Test\testing.txt");
string FullText;
using (StreamReader streamReader = new StreamReader(FilePath))
{
FullText = streamReader.ReadToEnd();
}
FileInfo Info = new FileInfo(FilePath);
int FileSize = Convert.ToInt32(Info.Length);
//some code
string[] Words = FullText.Split(' ');
var DistinctWords = new List<string>(Words.Distinct());
//some code
int P = 0;
int ID = 0;
int Length = 0;
int ByteWorth;
double Perc;
double PPerc = 0;
bool display = false;
using (var mappedFile1 = MemoryMappedFile.CreateFromFile(FilePath))
{
using (Stream mmStream = mappedFile1.CreateViewStream())
{
using (StreamReader sr = new StreamReader(mmStream, ASCIIEncoding.ASCII))
{
Parallel.ForEach(DistinctWords, new ParallelOptions { MaxDegreeOfParallelism = 1 }, Word =>
{
DataRow dr = dt.NewRow();
string SearchTerm = Word;
var MatchQuery = from word in Words
where word == SearchTerm
select word;
int WordCount = MatchQuery.Count();
Length = SearchTerm.Length;
if (Length > 1)
{
if (WordCount > 1)
{
ID = ID + 1;
ByteWorth = (Length * 8) * WordCount;
dr["Word"] = SearchTerm;
dr["Count"] = WordCount;
dr["ID"] = ID;
dr["Length"] = Length;
dr["ByteWorth"] = ByteWorth;
dt.Rows.Add(dr);
}
}
//some code below
这是迄今为止完整的应用程序......我知道不是很整洁。但我是编码新手。
欢迎任何提示,提示或建议。