Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.20 KB | None | 0 0
  1. class Program
  2. {
  3.  
  4.     internal static string CreateString(int stringLength)
  5.     {
  6.         Random rd = new Random();
  7.         const string allowedChars = "ABCDEFGHJKLMNOPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz0123456789!@$?_-";
  8.         char[] chars = new char[stringLength];
  9.  
  10.         for (int i = 0; i < stringLength; i++)
  11.         {
  12.             chars[i] = allowedChars[rd.Next(0, allowedChars.Length)];
  13.         }
  14.  
  15.         return new string(chars);
  16.     }
  17.  
  18.     static Dictionary<char, int> CountCharsInStr(string str)
  19.     {
  20.         return str
  21.             .Where(c => ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')))
  22.             .GroupBy(char.ToLower)
  23.             .ToDictionary(g => g.Key, g => g.Count());
  24.     }
  25.  
  26.     static void Main(string[] args)
  27.     {
  28.         string[] strings = new string[1000];
  29.         for (int i = 0; i < 1000; i++)
  30.         {
  31.             strings[i] = CreateString(10000);
  32.         }
  33.        
  34.  
  35.         var watch = System.Diagnostics.Stopwatch.StartNew();
  36.         for (int i = 0; i < 1000; i++)
  37.         {
  38.             CountCharsInStr(strings[i]);
  39.         }
  40.         watch.Stop();
  41.         var elapsedMs = watch.ElapsedMilliseconds;
  42.         Console.WriteLine(elapsedMs);
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement