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. $('#checkbox').attr('checked'); 返回的是checked或者是undefined解决办法

    $('#checkbox').attr('checked'); 返回的是checked或者是undefined解决办法 <input type='checkbox' id='cb'/>  ...

  2. thinkPHP开发基础知识 包括变量神马的

    hinkPHP框架开发的应用程序,一般都采用单一入口的方式,下面是在应用首页文件中实现的定义: 1.在首页定义thinkPHP框架路径 2.定义项目名称及路径,一般项目名称与项目文件夹名称保持一致 3 ...

  3. Java之线程———GUI线程(包含打字游戏和计时器俩个GUI实列)

    当java程序包含图形用户界面(GUI)时,Java虚拟机在运行应用程序时会自动启动更多的线程,其中有两个重要的线程:AWT-EventQuecue 和 AWT-Windows. AWT-EventQ ...

  4. hadoop2.0 和1.0的区别

    1. Hadoop 1.0中的资源管理方案Hadoop 1.0指的是版本为Apache Hadoop 0.20.x.1.x或者CDH3系列的Hadoop,内核主要由HDFS和MapReduce两个系统 ...

  5. Q: How could I use MATLAB interface for parameter selection?

    Q: How could I use MATLAB interface for parameter selection? One can do this by a simple loop. See t ...

  6. Objective-C:Foundation框架-常用类-NSNull

    集合中是不能存储nil值的,因为nil在集合中有特殊含义,但有时确实需要存储一个表示“什么都没有”的值,那么可以使用NSNull,它也是NSObject的一个子类. #import <Found ...

  7. VS简介

    visual studio2012 代码编程常用工具 1.起始页,存放一些方便打开的快捷方式,开始-新建项目-打开项目 2.最近-最近的项目 3.视图里面有一系列面板,窗口,比如起始页,工具箱,文档大 ...

  8. K最近邻

    k算法实现的步骤: 第一:确定K值(就是指最近邻居的个数).一般是一个奇数,因为测试样本个数有限, 第二:确定度量的长度,也就是余弦值,根据公式来算:     然后根据这个距离,排序大小,从中选出前k ...

  9. jsm使用

    参考:http://blog.csdn.net/robinjwong/article/details/38820259

  10. hover用法

    $('.F_box_2').hover(            function(){ $(this).find(".make_reply").css({"color&q ...