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和StartsWithui

2.判斷是否包含字符串url

IndexOf和Containsspa

3.判斷以字符串結尾pwa

LastIndexOf和EndsWith日誌

 

測試以某字符串爲開頭,以使用IndexOf爲最佳,有時,StartsWith也會比IndexOf快,概率較低。code

測試包含字符串,以Contains最佳。orm

測試以某字符串結尾,雖然LastIndexOf速度略快,可是很差斷定,仍是採用EndsWith爲最佳。htm

 

 

 

 

 

 

 

 

 

[C#]  純文本查看 複製代碼
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string url = "http://www.0398jobs.com/index.htm" ;
        private void Form1_Load( object sender, EventArgs e)
        {
 
        }
        //
        private void button1_Click( object sender, EventArgs e)
        {
            label1.Text = string .Empty;
            DateTime st = DateTime.Now;
            for ( int i = 0; i < 10000; i++)
            {
                if (url.IndexOf( "job.com" ) > 0 || url.IndexOf( "jobs.com" ) > 0)
                {
 
                }
            }
            label1.Text = (DateTime.Now - st).Milliseconds.ToString();
        }
        //Contains
        private void button2_Click( object sender, EventArgs e)
        {
            label1.Text = string .Empty;
            DateTime st = DateTime.Now;
            for ( int i = 0; i < 10000; i++)
            {
                if (url.Contains( "job.com" ) || url.Contains( "jobs.com" ))
                {
 
                }
            }
            label1.Text = (DateTime.Now-st).Milliseconds.ToString();
        }
    }



看測試後的效果

IndexOf執行五次

[C#]  純文本查看 複製代碼
?
01
02
03
04
05
06
38毫秒
36 毫秒
37毫秒
36毫秒
36毫秒
39毫秒



Contains執行五次

[C#]  純文本查看 複製代碼
?
01
02
03
04
05
06
3毫秒
2 毫秒
2毫秒
3毫秒
2毫秒
2毫秒

差距這麼大我就很少說了,你們一看就明白Contains的性能要遠遠的超出IndexOf測試源碼下載

相關文章
相關標籤/搜索