Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.63 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.  
  7. namespace SpeedCompare
  8. {
  9.     class Program
  10.     {
  11.         static Random random = new Random();
  12.         // static Random random = new Random(18347343);
  13.  
  14.         static String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToLower() + "0123456789";
  15.  
  16.         static String generateRandomString(int n)
  17.         {
  18.             return new String(Enumerable.Range(0, n).Select(_ => alphabet[random.Next(alphabet.Length)]).ToArray());
  19.         }
  20.  
  21.         static List<Tuple<String, String>> generateTestCase(int from, int to, int step, bool isSubstring, bool isRatioSize)
  22.         {
  23.             var res = new List<Tuple<String, String>>();
  24.             for (; from < to; from += step)
  25.             {
  26.                 int substringSize = isRatioSize ? from / 1000 : 1000;
  27.                 String longString = generateRandomString(from);
  28.                 String shortString = isSubstring
  29.                     // If short string should be a substring of a long string
  30.                     ? longString.Substring(random.Next(from - substringSize), substringSize)
  31.                     // othervise
  32.                     : generateRandomString(substringSize);
  33.  
  34.                 res.Add(new Tuple<String, String>(longString, shortString));
  35.             }
  36.             return res;
  37.         }
  38.  
  39.         static void PrintTestCase(List<Tuple<String, String>> testcase)
  40.         {
  41.             testcase.ForEach(t => Console.WriteLine(t.Item1 + "\t" + t.Item2));
  42.         }
  43.        
  44.         static List<R> printAlgorithmTimings<R, T>(List<Tuple<T, T>> testcase, Func<T, T, R> algorithm)
  45.         {
  46.             // Warmup cache
  47.             testcase.ForEach(arg => algorithm(arg.Item1, arg.Item2));
  48.             List<R> results = new List<R>(testcase.Count);
  49.             foreach(var arg in testcase)
  50.             {
  51.                 var watch = System.Diagnostics.Stopwatch.StartNew();
  52.                 var res = algorithm(arg.Item1, arg.Item2);
  53.                 watch.Stop();
  54.                 results.Add(res);
  55.                 Console.WriteLine(watch.Elapsed);
  56.             }
  57.             return results;
  58.         }
  59.  
  60.         static void compareTwoAlgorithms<R, T>(List<Tuple<T, T>> testcase, Func<T, T, R> first, Func<T, T, R> second)
  61.             where T: ICloneable
  62.         {
  63.             Console.WriteLine("First Algorithm Timings:");
  64.             List<R> firstResults = printAlgorithmTimings(testcase, first);
  65.            
  66.             Console.WriteLine("Second Algorithm Timings:");
  67.             List<R> secondResults = printAlgorithmTimings(testcase, first);
  68.  
  69.             // Compare, if two algorithms had the same output
  70.             if (!firstResults.Zip(secondResults, (x, y) => x.Equals(y)).Aggregate(true, (x, y) => x && y))
  71.             {
  72.                 throw new Exception("Algorithms had different output! Abort... ");
  73.             }
  74.         }
  75.  
  76.         static Boolean isSubstring1(String longString, String shortString)
  77.         {
  78.             return longString.Contains(shortString);
  79.         }
  80.  
  81.         static Boolean isSubstring2(String longString, String shortString)
  82.         {
  83.             /*
  84.              * Replace the string below with your code
  85.              */
  86.             return longString.Contains(shortString);
  87.         }
  88.        
  89.         static void compareSubstringAlgorithms(List<Tuple<String, String>> testcase)
  90.         {
  91.             compareTwoAlgorithms(testcase, isSubstring1, isSubstring2);
  92.         }
  93.  
  94.         static void Main(string[] args)
  95.         {
  96.             // var testcase = generateTestCase(30, 100, 10, isSubstring: false, isRatioSize: true);
  97.             // PrintTestCase(testcase);
  98.  
  99.             Console.WriteLine("######## isSubstring: false, isRatioSize: false ########");
  100.             compareSubstringAlgorithms(generateTestCase((int)10e6, (int)50e6, (int)5e6, isSubstring: false, isRatioSize: false));
  101.             Console.WriteLine("######## isSubstring: true, isRatioSize: false ########");
  102.             compareSubstringAlgorithms(generateTestCase((int)10e6, (int)50e6, (int)5e6, isSubstring: true, isRatioSize: false));
  103.             Console.WriteLine("######## isSubstring: false, isRatioSize: true ########");
  104.             compareSubstringAlgorithms(generateTestCase((int)10e6, (int)50e6, (int)5e6, isSubstring: false, isRatioSize: true));
  105.             Console.WriteLine("######## isSubstring: true, isRatioSize: true ########");
  106.             compareSubstringAlgorithms(generateTestCase((int)10e6, (int)50e6, (int)5e6, isSubstring: true, isRatioSize: true));
  107.        
  108.             Console.ReadKey();
  109.         }
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement