Judging whether a string is full of numbers and its performance comparison (C # implementation)

xiaoxiao2021-03-06  40

When programming, it is often encountered whether the characters in a string are all numbers (0-9), which is a very easy implementation, but the programmer will first think that this is the simple function? What can be used for ready-made functions? There is an isNumeric (Object) in VB.NET, only char.isnumber () that determines a single character () in C #, isNuMeric can determine the Double type of digital string, but cannot exclude the positive and decimal points, if the determination string is one The number of words is quite appropriate, but it cannot be used to determine if the string is composed of numbers. No ready-made method, I have to write a function: public static bool isnum (string str) {for (int i = 0; i

Regex isnumeric = new regex (@ "^ / d $"); int Times = 10000000; int Start, end; int I; string tobetested = "6741s";

#region Test user functionstart = System.Environment.TickCount; for (i = 0; i

#region Test Regular Expressionstart = System.Environment.TickCount; for (i = 0; i

#Region Test ExceptionStart = system.environment.tickcount; for (i = 0; i

} end = system.environment; console.writeline ("Exception Time:" (end-start) /10.0 "seconds"); # endregion # region test vb.net isnumeric () start = system.environment.tickcount ; for (i = 0; i

Because the time used by Exception is too long, only 1/100 is tested, which is not very rigorous, but the quantity level will not be wrong. The results of three runs is: User function Time: 1.938 SecondsRegular Expression Time: 11.921 SecondsException Time: 600 SecondsVB.NET IsNumeric () Time: 40.797 SecondsUser function Time: 1.953 SecondsRegular Expression Time: 12.016 SecondsException Time: 590.6 SecondsVB.NET IsNumeric () Time : 40 SecondsUser function time: 2.000 SecondsRegular Expression time: 12 SecondsException time: 595.3 SecondsVB.NET IsNumeric () time: 39.69 Seconds average time: sequentially 1.96411.979595.340.162 speed ratio of about: 303: 49.7: 1: 14.82 results are Obviously, the speed is the fastest, the speed is slower. If it does not need to throw exception string toBeTested = "67412"; it would result: User function Time: 1.922 SecondsRegular Expression Time: 9.64 SecondsException Time: 3.1 SecondsVB.NET IsNumeric () Time: 39.07 Seconds speed than the order of about: 20.33 : 4.05: 12.60: 1

Conclusion: Custom function can get the maximum flexibility and the highest performance, and complexity is not high, it is the best way. Regular expression method and ISNUMERIC speed are on the same order, but the regular expression can determine a string format, such as the specified must have or without decimal points, and ISNUMERIC cannot be done. Using an exception should be avoided. It is recommended to use Exception as a means of processing anomalies rather than as a means of control flow. Test also shows that when there is abnormally thrown, consume a lot of resources. IsNumeric is a ready-made function, which can only determine if the parameters given are values ​​(Boolean / Byte / INT16 / INT32 / INT64 / SINGLE / DOUBLE / DECIMAL), which cannot make further requirements (whether there is a decimal point, etc.) . But isnumeric's parameters are Object, which is not limited to String. Writing here, I don't want to think, is there a faster way than a self-defined function? The answer is yes. In the previous custom function, use the char.isnumber () function, which not only determines the '1' in the standard ASCII code, but even the '1' of the whole corner Chinese is also true, can be seen in char.isnumber ( The judgment is the number in all Unicode characters, and the numbers in other languages ​​are also included. If we only allow the '1' in ASCII, we can change this: public static bool isnum (string str) {for (int i = 0; i ' 9 ') Return False;} Return True;} The test results also surprised me, which increased nearly 10 times more than the original ISNUM speed, and the average execution time is 0.205 seconds!

The result is all out, how to choose everyone's heart, I have no count, I don't need to say anything. I wish you all a happy new year!

转载请注明原文地址:https://www.9cbs.com/read-71869.html

New Post(0)