惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The Register - Security
The Register - Security
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
MyScale Blog
MyScale Blog
V
Visual Studio Blog
云风的 BLOG
云风的 BLOG
aimingoo的专栏
aimingoo的专栏
C
Check Point Blog
J
Java Code Geeks
大猫的无限游戏
大猫的无限游戏
L
LangChain Blog
Vercel News
Vercel News
阮一峰的网络日志
阮一峰的网络日志
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
S
Security @ Cisco Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
人人都是产品经理
人人都是产品经理
H
Hacker News: Front Page
L
Lohrmann on Cybersecurity
T
Troy Hunt's Blog
T
Threat Research - Cisco Blogs
A
About on SuperTechFans
T
Threatpost
AWS News Blog
AWS News Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
T
Tor Project blog
Google Online Security Blog
Google Online Security Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Tenable Blog
W
WeLiveSecurity
博客园 - 叶小钗
K
Kaspersky official blog
Y
Y Combinator Blog
T
The Blog of Author Tim Ferriss
Hugging Face - Blog
Hugging Face - Blog
M
MIT News - Artificial intelligence
Hacker News - Newest:
Hacker News - Newest: "LLM"
Engineering at Meta
Engineering at Meta
有赞技术团队
有赞技术团队
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
S
Secure Thoughts
小众软件
小众软件
D
Docker
爱范儿
爱范儿
C
Cyber Attacks, Cyber Crime and Cyber Security
N
News and Events Feed by Topic
S
Schneier on Security
博客园 - 三生石上(FineUI控件)
D
DataBreaches.Net

博客园 - BloggerSb

Swagger 文档设置api版本 .Net Core Routing Demo .net Core读取配置比较 简化版DbExecutor,将DataTable映射到T属性(支持Dapper风格的匿名参数)。(编程题) ASP.NET Core CRUD API 创建 UserController,实现 Get, Post, Put, Delete 方法,使用 EF Core 访问数据库。 (编程题) 设计模式落地:Repository + UnitOfWork + CQRS 完整实现 (编程题) 给定百万级订单表,实现高效分页 + 动态条件查询 + 导出 Excel(避免内存爆炸) (编程题) 实现一个带 CorrelationId、请求日志、异常统一处理的中间件链 (编程题) 异步限流器实现(编程题) 编程题,记录所有接口的执行耗时 .net面试题目 (问答题) 面试高频简答题 Aspose最新Slides破解 HttpContext.User.Identity.IsAuthenticated 为false 关于Cannot resolve scoped service from root provider解决方案 MongoDB用户权限管理,设置密码并连接 mongodb连接字符串 mongodb 使用 MongoDB Compass 创建账号,角色 安装mongodb bootstrap popover 设置悬浮框宽度 div contenteditable="true" 添加placehoder效果 光标自动定位到起始位置contenteditable="true" ,v-html绑定内容,div可编辑时,光标移到最前面
大文件单词统计 (编程题)
BloggerSb · 2026-04-17 · via 博客园 - BloggerSb

大文件单词统计 

- 使用 StreamReader 分块读取,结合 Parallel.ForEach 并行统计,结果存入 ConcurrentDictionary<string,int>。 

using System;
using System.Collections.Concurrent;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;

class BigFileWordCounter
{
    static void Main()
    {
        // 替换成你的大文件路径
        string filePath = @"D:\large_file.txt";
        
        // 线程安全字典:存储最终统计结果
        var wordCount = new ConcurrentDictionary<string, int>(StringComparer.OrdinalIgnoreCase);

        Console.WriteLine("开始统计单词...");
        var startTime = DateTime.Now;

        // 1. 流式读取文件(分块,不占内存)
        var lines = File.ReadLines(filePath);

        // 2. 并行处理每一行
        Parallel.ForEach(lines, line =>
        {
            if (string.IsNullOrWhiteSpace(line)) return;

            // 正则提取单词(忽略标点、数字,只保留字母)
            var words = Regex.Matches(line, @"[a-zA-Z]+")
                             .Cast<Match>()
                             .Select(m => m.Value.ToLower()); // 统一小写,避免大小写重复统计

            // 3. 并行更新线程安全字典
            foreach (var word in words)
            {
                wordCount.AddOrUpdate(
                    key: word,
                    addValue: 1,           // 不存在则添加,值=1
                    updateValueFactory: (k, oldVal) => oldVal + 1 // 存在则+1
                );
            }
        });

        var endTime = DateTime.Now;
        Console.WriteLine($"\n统计完成!耗时:{(endTime - startTime).TotalSeconds:F2} 秒");
        Console.WriteLine($"不重复单词总数:{wordCount.Count}");

        // 输出前20个高频词(可选)
        Console.WriteLine("\n【出现频率最高的前20个单词】");
        foreach (var item in wordCount.OrderByDescending(kv => kv.Value).Take(20))
        {
            Console.WriteLine($"{item.Key,-15} {item.Value,6} 次");
        }
    }
}