GameDev/XNA/F#/and more…

Project Euler problem 99~
c#

  1:     class Program
  2:     {
  3:         static void Main(string[] args)
  4:         {
  5:             Console.WriteLine("The line number that has the greatest numerical value: ");
  6: 
  7:             TestBenchSetup();
  8:             TestBenchLoader(use_logarithm_and_formula_to_reduce_number_size);
  9: 
 10:             Console.WriteLine("press any key to exit");
 11:             Console.ReadKey();
 12:         }
 13: 
 14:         /// <summary>
 15:         /// using the formula logb(x^p) = p * logb(x), which makes the number fits into a double
 16:         /// </summary>
 17:         static int use_logarithm_and_formula_to_reduce_number_size()
 18:         {
 19:             var line_num = 0;
 20:             double max = 0;
 21:             var max_line = 0;
 22:             var base_exponent = new string[2];
 23:             double estimate = 0;
 24:             foreach (var line in File.ReadAllLines(@"..\..\base_exp.txt"))
 25:             {
 26:                 line_num++;
 27:                 base_exponent = line.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
 28:                 estimate = Convert.ToInt32(base_exponent[1]) * Math.Log10(Convert.ToInt32(base_exponent[0]));
 29: 
 30:                 if (estimate > max)
 31:                 {
 32:                     max = estimate;
 33:                     max_line = line_num;
 34:                 }
 35:             }
 36:             return max_line;
 37:         }
 38: 
 39:         static Stopwatch stopwatch = new Stopwatch();
 40:         static void TestBenchSetup()
 41:         {
 42:             // Uses the second Core or Processor for the Test
 43:             Process.GetCurrentProcess().ProcessorAffinity = new IntPtr(2);
 44:             // Prevents "Normal" processes from interrupting Threads
 45:             Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
 46:             // Prevents "Normal" Threads from interrupting this thread
 47:             Thread.CurrentThread.Priority = ThreadPriority.Highest;
 48:         }
 49:         // see http://www.codeproject.com/KB/testing/stopwatch-measure-precise.aspx
 50:         static void TestBenchLoader(Func<int> test_method)
 51:         {
 52:             stopwatch.Reset();
 53:             stopwatch.Start();
 54:             long result = 0;
 55:             long avg_tick = 0;
 56:             long avg_ms = 0;
 57:             while (stopwatch.ElapsedMilliseconds < 1200)  // A Warmup of 1000-1500 ms
 58:             // stabilizes the CPU cache and pipeline.
 59:             {
 60:                 result = test_method(); // Warmup
 61:             }
 62:             stopwatch.Stop();
 63:             for (int repeat = 0; repeat < 20; ++repeat)
 64:             {
 65:                 stopwatch.Reset();
 66:                 stopwatch.Start();
 67:                 result = test_method();
 68:                 stopwatch.Stop();
 69:                 avg_tick += stopwatch.ElapsedTicks;
 70:                 avg_ms += stopwatch.ElapsedMilliseconds;
 71:             }
 72:             avg_tick = avg_tick / 20;
 73:             avg_ms = avg_ms / 20;
 74:             Console.WriteLine(string.Format("{0} way(ticks:{1}, ms:{2}) Ans:{3}",
 75:                 test_method.Method.Name.Replace('_', ' '), avg_tick, avg_ms, result));
 76:         }
 77:     }

c++

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