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

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

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

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

IndexOf() LastIndexOf() Contains() StartsWith() EndsWith()方法比较的更多相关文章

  1. indexOf()、lastIndexOf()、startsWith()等方法应用

  2. python 中startswith()和endswith() 方法

    startswith()方法 Python startswith() 方法用于检查字符串是否是以指定子字符串开头如果是则返回 True,否则返回 False.如果参数 beg 和 end 指定值,则在 ...

  3. String中的Indexof,LastIndexOf, Indexofany,LastIndexOfAny 的区别

    本文转载自 http://www.cnblogs.com/qinying/archive/2008/09/22/1295730.html 定位子串是指在一个字符串中寻找其中包含的子串或者某个字符.在S ...

  4. Python endswith() 方法

    描述 endswith() 方法用于判断字符串是否以指定后缀结尾,如果是则返回 True,否则返回 False. 语法 endswith() 方法语法: S.endswith(suffix[,star ...

  5. 使用js的indexOf,lastIndexOf,slice三函数轻易得到url的服务器,路径和页名

    js的indexOf,lastIndexOf,slice能帮我们在js字符串处理时少走一些弯路. 程序如下: var url="http://www.cnblogs.com/xiandeda ...

  6. 数组方法indexOf & lastIndexOf

    indexOf() 语法:arrayObject.indexOf(searchvalue, startIndex) 功能:从数组的开头(位置0)开始向后查找. 参数:searchvalue:必需,要查 ...

  7. 字符串方法之-indexOf、lastIndexOf、等等一些方法

    1.indexOf():方法可返回某个指定的字符串值在字符串中首次出现的位置(从左往右找). 语法:stringObject.indexOf(searchvalue,fromindex) <sc ...

  8. js数组定义、属性及方法(push/pop/unshfit/shfit/reverse/sort/slice/splice/indexOf/lastIndexOf)

    数组 一.定义数组 * 字面量方式  var 数组名称 = [ value,value,... ] * 构造函数方式 var 数组名称 = new Array(value,value,...):  v ...

  9. 45-python基础-python3-字符串-常用字符串方法(三)-startswith()-endswith()

    4-字符串方法 startswith()和 endswith() startswith()和 endswith()判断字符串是否以某个字符串开始或结尾,存在返回 True,否则,方法返回 False. ...

随机推荐

  1. JavaWeb基础: Web应用和Web服务器

    Web Server工作原理 假设工程师想提供一个网页浏览的Web应用给客户,需要经过以下几步: 在指定目录下新建资源(hello.html) 编写一个服务器ServerDemo监听请求和响应请求:S ...

  2. MFC学习之窗口基础

    WinMain函数 1.句柄(HANDLE):{ 1. 定义:资源的标识 2. 句柄的作用: 操作系统通过句柄来找到对应的资源,从而对这些资源进行管理和操作. 3句柄的分类:(按资源){ 1.图标句柄 ...

  3. eclipse hibernate 插件测试1

    今天先测试了hibernate tools 安装 在eclipse marketplace里面搜索 hibernate tools 就能找到 网上很多文章所说的使用 install new softw ...

  4. Spark(2) - Developing Application with Spark

    Exploring the Spark shell Spark comes bundled with a PERL shell, which is a wrapper around the Scala ...

  5. Eclipse如何设置代码提示功能

    Windows→Preference→XML→XML Files→Editor→Content Assist→Auto Activation→Prompt when these characters ...

  6. 设置正确的post数据格式

    之前一直使用苏飞的HttpHelper类来访问网络,用起来一直感觉很爽.使用其工具直接生成访问代码很是方便.直到昨天下午做到需要使用wpf来post两个字段数据到服务器,服务器使用ASP.NET MV ...

  7. Asynchttpclient开源框架下载图片和文本,于Volley和Glide开源框架的区别。

    AsyncHttpClient是一款比较流行的Android异步网路加载库,在github上的网址是:https://github.com/loopj/android-async-httpAsyncH ...

  8. ARM2440 LCD实验

    1. S3C2440内部LCD控制器结构图: 我们根据数据手册来描述一下这个集成在S3C2440内部的LCD控制器: a:LCD控制器由REGBANK.LCDCDMA.TIMEGEN.VIDPRCS寄 ...

  9. HDU 5382 莫比乌斯反演

    题目大意: 求S(n)的值 n<=1000000 这是官方题解给出的推导过程,orz,按这上面说的来写,就不难了 这里需要思考的就是G(n)这个如何利用积性函数的性质线性筛出来 作为一个质数,那 ...

  10. HDU 4087 三维上的平移缩放旋转矩阵变化

    题目大意: 就是根据它给的程序的要求,不断平移,缩放,旋转三维的点,最后计算出点的位置 这里主要是要列出三种转换方式的齐次矩阵描述 平移translate tx ty tz1 0 0 00 1 0 0 ...