Advertisement
Guest User

Untitled

a guest
Sep 18th, 2013
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.36 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Diagnostics;
  7.  
  8. namespace ConsoleApplication15
  9. {
  10.  
  11.  
  12.  
  13.     class Program
  14.     {
  15.         static bool IsDigitsOnlyFOREACH(string str)
  16.         {
  17.             foreach (char c in str)
  18.             {
  19.                 if (c < '0' || c > '9')
  20.                     return false;
  21.             }
  22.  
  23.             return true;
  24.         }
  25.  
  26.         static bool IsDigitsOnlyFOR(string str)
  27.         {
  28.             for (int i = 0; i < str.Length;i++ )
  29.             {
  30.                 if (str[i] < '0' || str[i] > '9')
  31.                     return false;
  32.             }
  33.             return true;
  34.         }
  35.  
  36.         static bool IsDigitsOnlyALL(string str)
  37.         {
  38.             return str.All(c => c < '0' || c > '9');
  39.         }
  40.  
  41.         static bool IsDigitsCharIsDigit(string str)
  42.         {
  43.             return str.All(c => char.IsDigit(c));
  44.         }
  45.  
  46.         static bool IsDigitsOnlyPALL(string str)
  47.         {  
  48.             bool res=  false;
  49.             str.AsParallel().ForAll(
  50.                 c =>
  51.                 { if (c < '0' || c > '9') res = false;}
  52.                 );
  53.             return res;
  54.         }
  55.  
  56.         static void Main(string[] args)
  57.         {
  58.             string test = CreateTestData();
  59.  
  60.             TEST(IsDigitsOnlyFOREACH, test);
  61.             TEST(IsDigitsOnlyALL, test);
  62.             TEST(IsDigitsOnlyFOR, test);
  63.             TEST(IsDigitsOnlyPALL, test);
  64.             TEST(IsDigitsCharIsDigit, test);
  65.             Console.Read();
  66.  
  67.         }
  68.  
  69.         private static void TEST(Func<string,bool> cmd,string data)
  70.         {
  71.             var sw = new System.Diagnostics.Stopwatch();
  72.             sw.Start();
  73.             for (int i = 0; i < 1000; i++)
  74.             {
  75.                 Trace.Write(cmd.Invoke(data));
  76.             }
  77.             sw.Stop();
  78.             Console.WriteLine("method:" + cmd.Method.Name + " time: " +  sw.ElapsedMilliseconds);
  79.         }
  80.  
  81.         private static string CreateTestData()
  82.         {
  83.             string test = "123456677876965727625623565276863725623625362346527653836593783674567745454544276737763337367867342536578342657234657623498056062309";
  84.  
  85.             for (int i = 0; i < 12; i++)
  86.             {
  87.                 test += test;
  88.             }
  89.             return test;
  90.         }
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement