【原译】四种方法统计字符串的行数&执行时间比较
免责申明(必读!):本博客提供的所有教程的翻译原稿均来自于互联网,仅供学习交流之用,切勿进行商业传播。同时,转载时不要移除本申明。如产生任何纠纷,均与本博客所有人、发表该翻译稿之人无任何关系。谢谢合作!
原文链接地址:http://www.codeproject.com/Tips/312312/Counting-lines-in-a-string
我需要统计一下字符串的行数,因此我就写了一个超级没有技术含量的蛮力方法来统计了。
static long LinesCount(string s)
{
long count = 0;
int position = 0;
while ((position = s.IndexOf('\n', position)) != -1)
{
count++;
position++; // Skip this occurance!
}
return count;
}
这个函数他呀,运行正常,写起来也快。
但是,我就像啊,这是不是也太没有技术含量了,难道就没有其他方法了?
当然有,我想出了两种方法:正则和Linq,我把这些方法都写出来
static long LinesCountIndexOf(string s)
{
long count = 0;
int position = 0;
while ((position = s.IndexOf('\n', position)) != -1)
{
count++;
position++; // Skip this occurance!
}
return count;
}
static Regex r = new Regex("\n", RegexOptions.Multiline);
static long LinesCountRegex(string s)
{
MatchCollection mc = r.Matches(s);
return mc.Count;
}
static long LinesCountLinq(string s)
{
return (from ch in s
where ch== '\n'
select ch).Count();
}
static long LinesCountSplit(string s)
{
return (s.Split(new char[] { '\n' })).Length;
}
然后呢,我又写了一个快速但混乱的毫无技术含量的测试程序来测试正确性
string s = File.ReadAllText(@"D:\Temp\MyLargeTextFile.txt");
long index = LinesCountIndexOf(s);
long regex = LinesCountRegex(s);
long linq= LinesCountLinq(s);
Console.WriteLine("{0}:{1}:{2}", index, regex, linq);
Stopwatch si = new Stopwatch();
Stopwatch sd = new Stopwatch();
Stopwatch sl = new Stopwatch();
Stopwatch ss = new Stopwatch();
si.Start();
for (int i = 0; i < 100; i++)
{
index = LinesCountIndexOf(s);
}
si.Stop();
ss.Start();
for (int i = 0; i < 100; i++)
{
index = LinesCountSplit(s);
}
ss.Stop();
sd.Start();
for (int i = 0; i < 100; i++)
{
index = LinesCountRegex(s);
}
sd.Stop();
sl.Start();
for (int i = 0; i < 100; i++)
{
index = LinesCountLinq(s);
}
sl.Stop();
输入的文件是1.64Mb,包含大约23K行。
测试结果显示是
22777:22777:22777
有意思的是这个执行时间的结果(ms计)
Test ElapsedMilliseconds
BF+I 181
Split 1089
Regex 2557
Linq 3590
我本来想着这正则要快的不是一点点啊。正则和Linq这么大的差异令我震惊了,最令我震惊的是BF+I竟然比他们两个都快,而分割则毫无疑问比Index要慢,因为在分割方法中.net一次要分配23k的字符串空间
为了完成任务,我把BF+I版本重写了一个类,并且判断了字符串只有一行的情况,如你期望的一样,不要一秒就完成了
static class ExtensionMethods
{
/// <summary>
/// Returns the number of lines in a string
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static long Lines(this string s)
{
long count = 1;
int position = 0;
while ((position = s.IndexOf('\n', position)) != -1)
{
count++;
position++; // Skip this occurance!
}
return count;
}
}
注:count初始为1后,时间更短了一些。
Test ElapsedMilliseconds
BF+I 170
Split 1089
Regex 2063
Linq 3583
完成。。
著作权声明:本文由http://www.cnblogs.com/lazycoding翻译,欢迎转载分享。请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述