IndexOf() LastIndexOf() Contains() StartsWith() EndsWith()方法比较
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()方法比较的更多相关文章
- indexOf()、lastIndexOf()、startsWith()等方法应用
- python 中startswith()和endswith() 方法
startswith()方法 Python startswith() 方法用于检查字符串是否是以指定子字符串开头如果是则返回 True,否则返回 False.如果参数 beg 和 end 指定值,则在 ...
- String中的Indexof,LastIndexOf, Indexofany,LastIndexOfAny 的区别
本文转载自 http://www.cnblogs.com/qinying/archive/2008/09/22/1295730.html 定位子串是指在一个字符串中寻找其中包含的子串或者某个字符.在S ...
- Python endswith() 方法
描述 endswith() 方法用于判断字符串是否以指定后缀结尾,如果是则返回 True,否则返回 False. 语法 endswith() 方法语法: S.endswith(suffix[,star ...
- 使用js的indexOf,lastIndexOf,slice三函数轻易得到url的服务器,路径和页名
js的indexOf,lastIndexOf,slice能帮我们在js字符串处理时少走一些弯路. 程序如下: var url="http://www.cnblogs.com/xiandeda ...
- 数组方法indexOf & lastIndexOf
indexOf() 语法:arrayObject.indexOf(searchvalue, startIndex) 功能:从数组的开头(位置0)开始向后查找. 参数:searchvalue:必需,要查 ...
- 字符串方法之-indexOf、lastIndexOf、等等一些方法
1.indexOf():方法可返回某个指定的字符串值在字符串中首次出现的位置(从左往右找). 语法:stringObject.indexOf(searchvalue,fromindex) <sc ...
- js数组定义、属性及方法(push/pop/unshfit/shfit/reverse/sort/slice/splice/indexOf/lastIndexOf)
数组 一.定义数组 * 字面量方式 var 数组名称 = [ value,value,... ] * 构造函数方式 var 数组名称 = new Array(value,value,...): v ...
- 45-python基础-python3-字符串-常用字符串方法(三)-startswith()-endswith()
4-字符串方法 startswith()和 endswith() startswith()和 endswith()判断字符串是否以某个字符串开始或结尾,存在返回 True,否则,方法返回 False. ...
随机推荐
- Qt之图形视图框架
简述 图形视图(Graphics View)提供了一个平台,用于大量自定义2D图元的管理与交互,并提供了一个视图部件(view widget)来显示可以缩放和旋转的图元. 框架包括一个事件传播架构,支 ...
- javaSE之线程联合
首先定义 : 一个线程A在占有CPU资源期间 ,可以让其他线程调用join()和本线程联合. 嗯哈,像书本这个列子: 如: B.join(); 我们称A在运行期间联合了B, 如果线程A在占有CPU资源 ...
- 记一次web项目总结
功能需求,登录,用户管理,新闻管理. 用户管理: // 分页查询所有用户信息 public List<User> userInfo(int index, int pageSize) thr ...
- apache 访问权限基本设置
1 .禁止访问某个或多个文件夹 在.htaccess文件里面写入 RewriteRule ^foldername - [F,L] #禁止访问某个文件夹 RewriteRule ^ ...
- apache2.4域名配置参数
apache2.4和 2.2版本的配置有区别 2.4的配置如下 <VirtualHost *:80>DocumentRoot /www/web/projectServerName www. ...
- ASP.NET文章目录导航
ASP.NET文章目录导航 ASP.NET-[读书笔记]-原创:ASP.Net状态管理读书笔记--思维导图 (2013-12-25 10:13) ASP.NET-[潜在危险]-从客户端中检测到有潜在危 ...
- 项目中Enum枚举的使用
在.NET中,枚举一般有两种常见用法,一是表示唯一的元素序列,比如表示订单状态(未提交,待处理,处理中...).另外一种是表示多种组合的状态,比如表示权限,因为可同时有多个不同权限. 基本用法 这里拿 ...
- System.Web.Optimization找不到引用
在程序包管理控制程序中录入:Install-Package Microsoft.AspNet.Web.Optimization,安装即可.
- MVC HtmlHelper
HTML扩展类的所有方法都有2个参数: 以textbox为例子 public static string TextBox( this HtmlHelper htmlHelper, string nam ...
- Python 2.7教程
参考:http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000