Advertisement
Guest User

Untitled

a guest
Jul 7th, 2012
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.37 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Diagnostics;
  6.  
  7. namespace PieceOfKind
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             InitCtbl();
  14.             const int Tests = 5000000;
  15.  
  16.             double small_pure;
  17.             double small_libpow;
  18.             double small_iflibpow;
  19.             double small_ifbspow;
  20.             double small_ctblpow;
  21.  
  22.             double big_pure;
  23.             double big_libpow;
  24.             double big_iflibpow;
  25.             double big_ifbspow;
  26.             double big_ctblpow;
  27.  
  28.             Stopwatch sw = new Stopwatch();
  29.  
  30.             sw.Start();
  31.             int rnd = 2;
  32.             for (int i = 0; i < Tests; i++)
  33.             {
  34.                 rnd *= 59;
  35.                 LibPow(rnd / 123.0, rnd % 5);
  36.             }
  37.             sw.Stop();
  38.             small_libpow = sw.ElapsedTicks;
  39.  
  40.  
  41.             sw.Reset();
  42.             sw.Start();
  43.             rnd = 2;
  44.             for (int i = 0; i < Tests; i++)
  45.             {
  46.                 rnd *= 59;
  47.                 IfLibPow(rnd / 123.0, rnd % 5);
  48.             }
  49.             sw.Stop();
  50.             small_iflibpow = sw.ElapsedTicks;
  51.            
  52.  
  53.             sw.Reset();
  54.             sw.Start();
  55.             rnd = 2;
  56.             for (int i = 0; i < Tests; i++)
  57.             {
  58.                 rnd *= 59;
  59.                 IfBsearchLibPow(rnd / 123.0, rnd % 5);
  60.             }
  61.             sw.Stop();
  62.             small_ifbspow = sw.ElapsedTicks;
  63.  
  64.  
  65.             sw.Reset();
  66.             sw.Start();
  67.             rnd = 2;
  68.             for (int i = 0; i < Tests; i++)
  69.             {
  70.                 rnd *= 59;
  71.                 Math.Pow(rnd / 123.0, rnd % 5);
  72.             }
  73.             sw.Stop();
  74.             small_pure = sw.ElapsedTicks;
  75.  
  76.  
  77.             sw.Reset();
  78.             sw.Start();
  79.             rnd = 2;
  80.             for (int i = 0; i < Tests; i++)
  81.             {
  82.                 rnd *= 59;
  83.                 CtblLibPow(rnd / 123.0, rnd % 5);
  84.             }
  85.             sw.Stop();
  86.             small_ctblpow = sw.ElapsedTicks;
  87.            
  88.  
  89.  
  90.             sw.Reset();
  91.             sw.Start();
  92.             rnd = 2;
  93.             for (int i = 0; i < Tests; i++)
  94.             {
  95.                 rnd *= 59;
  96.                 LibPow(rnd / 123.0, rnd % 15);
  97.             }
  98.             sw.Stop();
  99.             big_libpow = sw.ElapsedTicks;
  100.  
  101.  
  102.             sw.Reset();
  103.             sw.Start();
  104.             rnd = 2;
  105.             for (int i = 0; i < Tests; i++)
  106.             {
  107.                 rnd *= 59;
  108.                 IfLibPow(rnd / 123.0, rnd % 15);
  109.             }
  110.             sw.Stop();
  111.             big_iflibpow = sw.ElapsedTicks;
  112.  
  113.  
  114.             sw.Reset();
  115.             sw.Start();
  116.             rnd = 2;
  117.             for (int i = 0; i < Tests; i++)
  118.             {
  119.                 rnd *= 59;
  120.                 IfBsearchLibPow(rnd / 123.0, rnd % 15);
  121.             }
  122.             sw.Stop();
  123.             big_ifbspow = sw.ElapsedTicks;
  124.  
  125.  
  126.             sw.Reset();
  127.             sw.Start();
  128.             rnd = 2;
  129.             for (int i = 0; i < Tests; i++)
  130.             {
  131.                 rnd *= 59;
  132.                 CtblLibPow(rnd / 123.0, rnd % 15);
  133.             }
  134.             sw.Stop();
  135.             big_ctblpow = sw.ElapsedTicks;
  136.  
  137.             sw.Reset();
  138.             sw.Start();
  139.             rnd = 2;
  140.             for (int i = 0; i < Tests; i++)
  141.             {
  142.                 rnd *= 59;
  143.                 Math.Pow(rnd / 123.0, rnd % 15);
  144.             }
  145.             sw.Stop();
  146.             big_pure = sw.ElapsedTicks;
  147.  
  148.             Console.WriteLine("Small\tPurepow: \t{0}", small_pure);
  149.             Console.WriteLine("Small\tLibpow: \t{0}", small_libpow);
  150.             Console.WriteLine("Small\tIfLibpow: \t{0}", small_iflibpow);
  151.             Console.WriteLine("Small\tIfbsLibpow: \t{0}", small_ifbspow);
  152.             Console.WriteLine("Small\tCtbLibpow: \t{0}", small_ctblpow);
  153.             Console.WriteLine("Big\tPurepow: \t{0}", big_pure);
  154.             Console.WriteLine("Big\tLibpow: \t{0}", big_libpow);
  155.             Console.WriteLine("Big\tIfLibpow: \t{0}", big_iflibpow);
  156.             Console.WriteLine("Big\tIfbsLibpow: \t{0}", big_ifbspow);
  157.             Console.WriteLine("Big\tCtbLibpow: \t{0}", big_ctblpow);
  158.  
  159.             Console.ReadKey(true);
  160.         }
  161.  
  162.         static double LibPow(double x, int n)
  163.         {
  164.             if (n < 0)
  165.             { return (1.0 / LibPow(x, -n)); }
  166.  
  167.             switch (n)
  168.             {
  169.                 case 0:
  170.                 { return (1.0); }
  171.                 case 1:
  172.                 { return (x); }
  173.                 case 2:
  174.                 { return (x * x); }
  175.                 case 3:
  176.                 { return (x * x * x); }
  177.                 case 4:
  178.                 {
  179.                     double x2 = x * x;
  180.                     return (x2 * x2);
  181.                 }                case 5:
  182.                 {
  183.                     double x2 = x * x;
  184.                     return (x2 * x2 * x);
  185.                 }
  186.                 case 6:
  187.                 {
  188.                     double x2 = x * x;
  189.                     return (x2 * x2 * x2);
  190.                 }
  191.                 case 7:
  192.                 {
  193.                     double x3 = x * x * x;
  194.                     return (x3 * x3 * x);
  195.                 }
  196.                 case 8:
  197.                 {
  198.                     double x2 = x * x;
  199.                     double x4 = x2 * x2;
  200.                     return (x4 * x4);
  201.                 }
  202.                 case 9:
  203.                 {
  204.                     double x3 = x * x * x;
  205.                     return (x3 * x3 * x3);
  206.                 }
  207.                 case 10:
  208.                 {
  209.                     double x2 = x * x;
  210.                     double x4 = x2 * x2;
  211.                     return (x4 * x4 * x2);
  212.                 }
  213.                 default:
  214.                 { return (Math.Pow(x, n)); }
  215.             }
  216.         }
  217.  
  218.         static double IfLibPow(double x, int n)
  219.         {
  220.             if (n < 0)
  221.             { return (1.0 / IfLibPow(x, -n)); }
  222.  
  223.             if (n == 0)
  224.             { return (1.0); }
  225.             else if (n == 1)
  226.             { return (x); }
  227.             else if (n == 2)
  228.             { return (x * x); }
  229.             else if (n == 3)
  230.             { return (x * x * x); }
  231.             else if (n == 4)
  232.             {
  233.                 double x2 = x * x;
  234.                 return (x2 * x2);
  235.             }
  236.             else if (n == 5)
  237.             {
  238.                 double x2 = x * x;
  239.                 return (x2 * x2 * x);
  240.             }
  241.             else if (n == 6)
  242.             {
  243.                 double x2 = x * x;
  244.                 return (x2 * x2 * x2);
  245.             }
  246.             else if (n == 7)
  247.             {
  248.                 double x3 = x * x * x;
  249.                 return (x3 * x3 * x);
  250.             }
  251.             else if (n == 8)
  252.             {
  253.                 double x2 = x * x;
  254.                 double x4 = x2 * x2;
  255.                 return (x4 * x4);
  256.             }
  257.             else if (n == 9)
  258.             {
  259.                 double x3 = x * x * x;
  260.                 return (x3 * x3 * x3);
  261.             }
  262.             else if (n == 10)
  263.             {
  264.                 double x2 = x * x;
  265.                 double x4 = x2 * x2;
  266.                 return (x4 * x4 * x2);
  267.             }
  268.             else
  269.                 return (Math.Pow(x, n));
  270.         }
  271.  
  272.         static double IfBsearchLibPow(double x, int n)
  273.         {
  274.             if (n <= 5)
  275.                 if (n <= 1)
  276.                     if (n < 0)
  277.                         return (1.0 / IfBsearchLibPow(x, -n)); // < 0
  278.                     else
  279.                         if (n == 0)
  280.                             return 1; // 0
  281.                         else
  282.                             return x; // 1
  283.                 else
  284.                     if (n <= 3)
  285.                         if (n == 2)
  286.                             return (x * x); // 2
  287.                         else
  288.                             return (x * x * x); // 3
  289.                     else
  290.                         if (n == 4)
  291.                         {
  292.                             double x2 = x * x;
  293.                             return (x2 * x2);
  294.                         }
  295.                         else // 5
  296.                         {
  297.                             double x2 = x * x;
  298.                             return (x2 * x2 * x);
  299.                         }
  300.             else
  301.                 if (n <= 9)
  302.                     if (n <= 7)
  303.                         if (n == 6) // 6
  304.                         {
  305.                             double x2 = x * x;
  306.                             return (x2 * x2 * x2);
  307.                         }
  308.                         else // 7
  309.                         {
  310.                             double x3 = x * x * x;
  311.                             return (x3 * x3 * x);
  312.                         }
  313.                     else
  314.                         if (n == 8) // 8
  315.                         {
  316.                             double x2 = x * x;
  317.                             double x4 = x2 * x2;
  318.                             return (x4 * x4);
  319.                         }
  320.                         else // 9
  321.                         {
  322.                             double x3 = x * x * x;
  323.                             return (x3 * x3 * x3);
  324.                         }
  325.                 else
  326.                     if (n == 10) // 10
  327.                     {
  328.                         double x2 = x * x;
  329.                         double x4 = x2 * x2;
  330.                         return (x4 * x4 * x2);
  331.                     }
  332.                     else // > 10
  333.                         return Math.Pow(x, n);
  334.         }
  335.  
  336.         static double CtblLibPow(double x, int n)
  337.         {
  338.             if (n < 0)
  339.             { return (1.0 / CtblLibPow(x, -n)); }
  340.  
  341.             if (n > 10)
  342.             { return Math.Pow(x, n); }
  343.  
  344.             return Ctbl[n](x);
  345.         }
  346.  
  347.         static void InitCtbl()
  348.         {
  349.             Ctbl = new List<Func<double, double>>();
  350.             Ctbl.Add((x) => 1);
  351.             Ctbl.Add((x) => x);
  352.             Ctbl.Add((x) =>
  353.                             { return (x * x); });
  354.             Ctbl.Add((x) =>
  355.             { return (x * x * x); });
  356.             Ctbl.Add((x) =>
  357.                  {
  358.                      double x2 = x * x;
  359.                      return (x2 * x2);
  360.                  });
  361.             Ctbl.Add((x) =>
  362.                    {
  363.                        double x2 = x * x;
  364.                        return (x2 * x2 * x);
  365.                    });
  366.             Ctbl.Add((x) =>
  367.                    {
  368.                        double x2 = x * x;
  369.                        return (x2 * x2 * x2);
  370.                    });
  371.             Ctbl.Add((x) =>
  372.                   {
  373.                       double x3 = x * x * x;
  374.                       return (x3 * x3 * x);
  375.                   });
  376.             Ctbl.Add((x) =>
  377.                   {
  378.                       double x2 = x * x;
  379.                       double x4 = x2 * x2;
  380.                       return (x4 * x4);
  381.                   });
  382.             Ctbl.Add((x) =>
  383.                 {
  384.                     double x3 = x * x * x;
  385.                     return (x3 * x3 * x3);
  386.                 });
  387.             Ctbl.Add((x) =>
  388.                 {
  389.                     double x2 = x * x;
  390.                     double x4 = x2 * x2;
  391.                     return (x4 * x4 * x2);
  392.                 });
  393.         }
  394.  
  395.         static List<Func<double, double>> Ctbl;
  396.     }
  397. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement