今天在寫**時突然想起測試經常用microsoft.visualbasic.information.isnumeric判斷 url引數是否為數字時的這個方法的效率
因為數字是字串是直接使用的,所以不需要轉型,也就沒有用tryparse
結果一測試嚇一跳,這個方法的效率是如此的低,再測試了下tryparse還不錯,正則的也比較差,
沒什麼技術含量,看結果吧:
先拓展下字串:
code
public
static
class
common
return
true;}
//vb isnumberic
public
static
bool
isnumberic2(
this
string
_string)
//try parse
public
static
bool
isnumberic3(
this
string
_string)
//try catch
public
static
bool
isnumberic4(
this
string
_string)
catch
return
true;}
//regex
public
static
bool
isnumberic5(
this
string
_string)
}測試的**:
code
class
program
static
void
test(
string
str)
wat.stop();
console.writeline(
"isdigit :,
", str, wat.elapsedmilliseconds, res1);
wat.reset();
wat.start();
for(
inti =1
; i
<
100000
; i++
)wat.stop();
console.writeline(
"isnumberic :,
", str, wat.elapsedmilliseconds, res2);
wat.reset();
wat.start();
for(
inti =1
; i
<
100000
; i++
)wat.stop();
console.writeline(
"try parse :,
", str, wat.elapsedmilliseconds, res3);
wat.reset();
wat.start();
for(
inti =1
; i
<
100000
; i++
)wat.stop();
console.writeline(
"try catch :,
", str, wat.elapsedmilliseconds, res4);
wat.reset();
wat.start();
for(
inti =1
; i
<
100000
; i++
)wat.stop();
console.writeline(
"regex :,
", str, wat.elapsedmilliseconds, res5);
console.writeline();}}
下面是我本機的測試結果
isdigit 1234:5,true
isnumberic 1234:166,true
try parse 1234:21,true
try catch 1234:22,true
regex 1234:134,true
isdigit 1234a:5,false
isnumberic 1234a:196,false
try parse 1234a:19,false
try catch 1234a:5182,false
regex 1234a:150,false
isdigit a1234:2,false
isnumberic a1234:184,false
try parse a1234:16,false
try catch a1234:5084,false
regex a1234:106,false
isdigit :1,false
isnumberic :0,false
try parse :0,false
try catch :1,false
regex :0,false
isdigit :1,false
isnumberic :0,false
try parse :1,false
try catch :1,false
regex :0,false
結果:迴圈判斷是否是數字字元效率是最高的
而visualbasic的方法效率比較低了
順便測試了下visualbasic裡的left和right方法效率也一樣的低,還不及substring的十分之一
所以visualbasic裡雖然有幾個方法從名字看來比較好用,實際卻比較杯具。
c 判斷乙個字串是否包含另乙個字串
c 開發過程中針對字串string型別的操作是常見操作,有時候需要判斷某個字串是否包含在另乙個字串,此時可以使用indexof方法以及contain方法來實現此功能,contain方法返回true和false,而indexof方法可以返回所在的索引位置,如果為查詢到對應的字串,則返回 1。c 中字串...
判斷乙個字串是否是另乙個字串的子集
案例 c 開發 遇到模糊查詢的功能實現,不是直接查詢資料庫,而是匹配另外一些本地資料。string a asd string b asdsdad 其實a b的值都是取得動態資料。一開始自己寫了乙個函式用來判斷a是否在b中,for 迴圈即可。後來,才知道.net平台有這樣的函式,判斷字串的子集的索引號...
SQL 判斷乙個字串是否在另外乙個字串中
eg str1 admin str2 1234,123admin,xcxx 比較str1是否在str2中 用常用的charindex,返回肯定是有值的,這裡自己動手寫乙個方法 檢查乙個字串是否在另外乙個字串中數,另外乙個字串元素用,隔開 create function dbo checkstrina...