cjdxhc_space

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

 

代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace CA100
{
class Program
{
//循环次数:5百万次
const int COUNT = 5000000;
//外围循环次数:5次
const int NUM = 5;
//准确测量运行时间
static Stopwatch sWatch = new Stopwatch();
//每项时间
static long t1, t2;
//记录日志
static StringBuilder sb = new StringBuilder();

static void Main()
{
string src = "C#测试IndexOf()LastIndexOf()Contains()StartsWith()EndsWith()5个方法的效率.";
Console.WriteLine(
"测试的字符串是:" + src + ",测试次数" + COUNT);
sb.AppendLine(
"测试的字符串是:" + src + ",测试次数" + COUNT);
string str = "C";
//每项循环测试5次
int i = 0;
Console.WriteLine(
"\n'C'出现在首位时:\n");
sb.AppendLine(
"\r\n'C'出现在首位时:\r\n");
for (; i < NUM; i++)
{
Console.WriteLine(
"当前循环第{0}次\n", i + 1);
sb.AppendLine(
"当前循环第" + (i + 1) + "");
t1
+= IndexOf(src, str);
t2
+= StartsWith(src, str);
Console.WriteLine();
sb.AppendLine();
}
Console.WriteLine(
"IndexOf总时间:" + t1 + ",平均时间:" + t1 / NUM);
sb.AppendLine(
"IndexOf总时间:" + t1 + ",平均时间:" + t1 / NUM);
Console.WriteLine(
"StartsWith总时间:" + t2 + ",平均时间:" + t2 / NUM);
sb.AppendLine(
"StartsWith总时间:" + t2 + ",平均时间:" + t2 / NUM);
Console.WriteLine();
sb.AppendLine();
t1
= 0;
t2
= 0;

str
= "StartsWith";
Console.WriteLine(
"'StartsWith'出现在中间:\n");
sb.AppendLine(
"'StartsWith'出现在中间:\r\n");
for (i = 0; i < NUM; i++)
{
Console.WriteLine(
"当前循环第{0}次\n", i + 1);
sb.AppendLine(
"当前循环第" + (i + 1) + "");
t1
+= IndexOf(src, str);
t2
+= Contains(src, str);
Console.WriteLine();
sb.AppendLine();
}

Console.WriteLine(
"IndexOf总时间:" + t1 + ",平均时间:" + t1 / NUM);
sb.AppendLine(
"IndexOf总时间:" + t1 + ",平均时间:" + t1 / NUM);
Console.WriteLine(
"Contains总时间:" + t2 + ",平均时间:" + t2 / NUM);
sb.AppendLine(
"Contains总时间:" + t2 + ",平均时间:" + t2 / NUM);
Console.WriteLine();
sb.AppendLine();
t1
= 0;
t2
= 0;

str
= ".";
Console.WriteLine(
"'.'出现在末尾:\n");
sb.AppendLine(
"'.'出现在末尾:\r\n");
for (i = 0; i < NUM; i++)
{
Console.WriteLine(
"当前循环第{0}次\n", i + 1);
sb.AppendLine(
"当前循环第" + (i + 1) + "");
t1
+= LastIndexOf(src, str);
t2
+= EndsWith(src, str);
Console.WriteLine();
sb.AppendLine();
}

Console.WriteLine(
"LastIndexOf总时间:" + t1 + ",平均时间:" + t1 / NUM);
sb.AppendLine(
"LastIndexOf总时间:" + t1 + ",平均时间:" + t1 / NUM);
Console.WriteLine(
"EndsWith总时间:" + t2 + ",平均时间:" + t2 / NUM);
sb.AppendLine(
"EndsWith总时间:" + t2 + ",平均时间:" + t2 / NUM);
Console.WriteLine();
sb.AppendLine();

Console.WriteLine(
"测试结束!");
sb.AppendLine(
"测试结束!");

File.AppendAllText(
@"d:\results.txt", sb.ToString());
Console.ReadLine();
}

static long IndexOf(string src, string str)
{
sWatch.Reset();
sWatch.Start();
for (int i = 0; i < COUNT; i++)
{
src.IndexOf(str);
}
sWatch.Stop();

Console.WriteLine(
"IndexOf花费: " + sWatch.ElapsedMilliseconds + "ms");
sb.AppendLine(
"IndexOf花费: " + sWatch.ElapsedMilliseconds + "ms");
return sWatch.ElapsedMilliseconds;
}

static long LastIndexOf(string src, string str)
{
sWatch.Reset();
sWatch.Start();
for (int i = 0; i < COUNT; i++)
{
src.LastIndexOf(str);
}
sWatch.Stop();

Console.WriteLine(
"LastIndexOf花费: " + sWatch.ElapsedMilliseconds + "ms");
sb.AppendLine(
"LastIndexOf花费: " + sWatch.ElapsedMilliseconds + "ms");
return sWatch.ElapsedMilliseconds;
}

static long StartsWith(string src, string str)
{
sWatch.Reset();
sWatch.Start();
for (int i = 0; i < COUNT; i++)
{
src.StartsWith(str);
}
sWatch.Stop();

Console.WriteLine(
"StartsWith花费: " + sWatch.ElapsedMilliseconds + "ms");
sb.AppendLine(
"StartsWith花费: " + sWatch.ElapsedMilliseconds + "ms");
return sWatch.ElapsedMilliseconds;
}

static long EndsWith(string src, string str)
{
sWatch.Reset();
sWatch.Start();
for (int i = 0; i < COUNT; i++)
{
src.EndsWith(str);
}
sWatch.Stop();

Console.WriteLine(
"EndsWith花费: " + sWatch.ElapsedMilliseconds + "ms");
sb.AppendLine(
"EndsWith花费: " + sWatch.ElapsedMilliseconds + "ms");
return sWatch.ElapsedMilliseconds;
}

static long Contains(string src, string str)
{
sWatch.Reset();
sWatch.Start();
for (int i = 0; i < COUNT; i++)
{
src.Contains(str);
}
sWatch.Stop();

Console.WriteLine(
"Contains花费: " + sWatch.ElapsedMilliseconds + "ms");
sb.AppendLine(
"Contains花费: " + sWatch.ElapsedMilliseconds + "ms");
return sWatch.ElapsedMilliseconds;
}
}
}

 

针对三种情况

1.判断以字符串开头

IndexOf和StartsWith

2.判断是否包含字符串

IndexOf和Contains

3.判断以字符串结尾

LastIndexOf和EndsWith

 

测试系统:XP SP3

处理器:Pentium E2140 1.6GHz 双核

内存:1G

 

用VS2010 创建了控制台项目,一个解决方案包含四个项目(CA80、CA90、CA95、CA100),看名称表示ConsoleApplication+(C#2.0-4.0)。使用的代码完全一致,但是在不同的.net framework下运行速度不同。

 

测试后发现,在2.0-3.5之间,测试结果相似。(以下单位是:ms)

IndexOf总时间:5483,平均时间:1096
StartsWith总时间:5723,平均时间:1144

IndexOf总时间:43366,平均时间:8673
Contains总时间:7383,平均时间:1476

LastIndexOf总时间:5349,平均时间:1069
EndsWith总时间:5618,平均时间:1123

 

测试以某字符串为开头,以使用IndexOf为最佳,有时,StartsWith也会比IndexOf快,几率较低。

测试包含字符串,以Contains最佳。

测试以某字符串结尾,虽然LastIndexOf速度略快,但是不好判定,还是采用EndsWith为最佳。

 

但是在4.0下,结果确惊人的不同。(以下单位是:ms)

IndexOf总时间:8169,平均时间:1633
StartsWith总时间:8419,平均时间:1683

IndexOf总时间:120224,平均时间:24044
Contains总时间:6064,平均时间:1212

LastIndexOf总时间:48432,平均时间:9686
EndsWith总时间:48418,平均时间:9683

 

虽然最终的结果差不多,但是速度运气起来真的是差的恐怖。

这到底是为什么呢?

posted on 2010-04-29 15:25  cjdxhc  阅读(8701)  评论(4编辑  收藏  举报