Advertisement
ivandrofly

char.IsDigit vs Expression

Mar 1st, 2015
374
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.53 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Diagnostics;
  4.  
  5. namespace ConsoleApplication1
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             var stopwatch = Stopwatch.StartNew();
  12.             const int times = 100000000;
  13.             for (int i = 0; i < times; i++)
  14.             {
  15.                 var output = RemoveNonNumbers1("oa9sd01234567897fa9");
  16.             }
  17.             stopwatch.Stop();
  18.             Console.WriteLine(stopwatch.Elapsed);
  19.             stopwatch.Restart();
  20.             for (int i = 0; i < times; i++)
  21.             {
  22.                 var output = RemoveNonNumbers2("oa9sd01234567897fa9");
  23.             }
  24.             stopwatch.Stop();
  25.             Console.WriteLine(stopwatch.Elapsed);
  26.             Console.ReadLine();
  27.         }
  28.  
  29.         internal static string RemoveNonNumbers1(string p)
  30.         {
  31.             if (string.IsNullOrEmpty(p))
  32.                 return p;
  33.  
  34.             var sb = new StringBuilder();
  35.             foreach (var c in p)
  36.             {
  37.                 if (c >= 0x30 && c <= 0x39)
  38.                     sb.Append(c);
  39.             }
  40.             return sb.ToString();
  41.         }
  42.  
  43.         internal static string RemoveNonNumbers2(string p)
  44.         {
  45.             if (string.IsNullOrEmpty(p))
  46.                 return p;
  47.  
  48.             var sb = new StringBuilder();
  49.             foreach (var c in p)
  50.             {
  51.                 if (char.IsDigit(c))
  52.                     sb.Append(c);
  53.             }
  54.             return sb.ToString();
  55.         }
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement