Rx/ExpressionBlend/c#/and more…

Project Euler problem 42~

  1:     class Program
  2:     {
  3:         static void Main(string[] args)
  4:         {
  5:             Console.WriteLine("The number of triangle words:");
  6: 
  7:             TestBenchSetup();
  8:             TestBenchLoader(using_linq_to_make_my_life_easier);
  9: 
 10:             Console.WriteLine("Press any key to exit");
 11:             Console.ReadKey();
 12:         }
 13: 
 14:         /// <summary>
 15:         /// not sure why this is sooo fast(2500ish ticks on my i7 920, with intel ssd)
 16:         /// note to self: try to find out whats going on under the hood, rather than spending time on reddit
 17:         /// </summary>
 18:         /// <returns></returns>
 19:         static int using_linq_to_make_my_life_easier()
 20:         {
 21:             var nums = new HashSet<int>();
 22: 
 23:             Enumerable.Range(1, 30) // see http://en.wikipedia.org/wiki/Longest_word_in_English
 24:                 .Select(l => l * (l + 1) / 2)
 25:                 .ToList()
 26:                 .ForEach(l => nums.Add(l));
 27: 
 28:             return File.ReadAllText("words.txt").Split(new char[] { ',' })
 29:                 .Select(l => l.Trim(new char[] { '"' }).ToCharArray().Aggregate(0, (s, n) => s += (Convert.ToInt32(n) - 64)))
 30:                 .Where(l => nums.Contains(l))
 31:                 .Count();
 32:         }
 33: 
 34:         static Stopwatch stopwatch = new Stopwatch();
 35:         static void TestBenchSetup()
 36:         {
 37:             // Uses the second Core or Processor for the Test
 38:             Process.GetCurrentProcess().ProcessorAffinity = new IntPtr(2);
 39:             // Prevents "Normal" processes from interrupting Threads
 40:             Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
 41:             // Prevents "Normal" Threads from interrupting this thread
 42:             Thread.CurrentThread.Priority = ThreadPriority.Highest;
 43:         }
 44:         // see http://www.codeproject.com/KB/testing/stopwatch-measure-precise.aspx
 45:         static void TestBenchLoader(Func<int> test_method)
 46:         {
 47:             stopwatch.Reset();
 48:             stopwatch.Start();
 49:             var result = 0;
 50:             long avg_tick = 0;
 51:             long avg_ms = 0;
 52:             while (stopwatch.ElapsedMilliseconds < 1200)  // A Warmup of 1000-1500 ms 
 53:             // stabilizes the CPU cache and pipeline.
 54:             {
 55:                 result = test_method(); // Warmup
 56:             }
 57:             stopwatch.Stop();
 58:             for (int repeat = 0; repeat < 20; ++repeat)
 59:             {
 60:                 stopwatch.Reset();
 61:                 stopwatch.Start();
 62:                 result = test_method();
 63:                 stopwatch.Stop();
 64:                 avg_tick += stopwatch.ElapsedTicks;
 65:                 avg_ms += stopwatch.ElapsedMilliseconds;
 66:             }
 67:             avg_tick = avg_tick / 20;
 68:             avg_ms = avg_ms / 20;
 69:             Console.WriteLine(string.Format("{0} way(ticks:{1}, ms:{2}) Ans:{3}",
 70:                 test_method.Method.Name.Replace('_', ' '), avg_tick, avg_ms, result));
 71:         }
 72:     }

Advertisements
This entry was posted in Programming. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s