Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Diagnostics;
- namespace PieceOfKind
- {
- class Program
- {
- static void Main(string[] args)
- {
- InitCtbl();
- const int Tests = 5000000;
- double small_pure;
- double small_libpow;
- double small_iflibpow;
- double small_ifbspow;
- double small_ctblpow;
- double big_pure;
- double big_libpow;
- double big_iflibpow;
- double big_ifbspow;
- double big_ctblpow;
- Stopwatch sw = new Stopwatch();
- sw.Start();
- int rnd = 2;
- for (int i = 0; i < Tests; i++)
- {
- rnd *= 59;
- LibPow(rnd / 123.0, rnd % 5);
- }
- sw.Stop();
- small_libpow = sw.ElapsedTicks;
- sw.Reset();
- sw.Start();
- rnd = 2;
- for (int i = 0; i < Tests; i++)
- {
- rnd *= 59;
- IfLibPow(rnd / 123.0, rnd % 5);
- }
- sw.Stop();
- small_iflibpow = sw.ElapsedTicks;
- sw.Reset();
- sw.Start();
- rnd = 2;
- for (int i = 0; i < Tests; i++)
- {
- rnd *= 59;
- IfBsearchLibPow(rnd / 123.0, rnd % 5);
- }
- sw.Stop();
- small_ifbspow = sw.ElapsedTicks;
- sw.Reset();
- sw.Start();
- rnd = 2;
- for (int i = 0; i < Tests; i++)
- {
- rnd *= 59;
- Math.Pow(rnd / 123.0, rnd % 5);
- }
- sw.Stop();
- small_pure = sw.ElapsedTicks;
- sw.Reset();
- sw.Start();
- rnd = 2;
- for (int i = 0; i < Tests; i++)
- {
- rnd *= 59;
- CtblLibPow(rnd / 123.0, rnd % 5);
- }
- sw.Stop();
- small_ctblpow = sw.ElapsedTicks;
- sw.Reset();
- sw.Start();
- rnd = 2;
- for (int i = 0; i < Tests; i++)
- {
- rnd *= 59;
- LibPow(rnd / 123.0, rnd % 15);
- }
- sw.Stop();
- big_libpow = sw.ElapsedTicks;
- sw.Reset();
- sw.Start();
- rnd = 2;
- for (int i = 0; i < Tests; i++)
- {
- rnd *= 59;
- IfLibPow(rnd / 123.0, rnd % 15);
- }
- sw.Stop();
- big_iflibpow = sw.ElapsedTicks;
- sw.Reset();
- sw.Start();
- rnd = 2;
- for (int i = 0; i < Tests; i++)
- {
- rnd *= 59;
- IfBsearchLibPow(rnd / 123.0, rnd % 15);
- }
- sw.Stop();
- big_ifbspow = sw.ElapsedTicks;
- sw.Reset();
- sw.Start();
- rnd = 2;
- for (int i = 0; i < Tests; i++)
- {
- rnd *= 59;
- CtblLibPow(rnd / 123.0, rnd % 15);
- }
- sw.Stop();
- big_ctblpow = sw.ElapsedTicks;
- sw.Reset();
- sw.Start();
- rnd = 2;
- for (int i = 0; i < Tests; i++)
- {
- rnd *= 59;
- Math.Pow(rnd / 123.0, rnd % 15);
- }
- sw.Stop();
- big_pure = sw.ElapsedTicks;
- Console.WriteLine("Small\tPurepow: \t{0}", small_pure);
- Console.WriteLine("Small\tLibpow: \t{0}", small_libpow);
- Console.WriteLine("Small\tIfLibpow: \t{0}", small_iflibpow);
- Console.WriteLine("Small\tIfbsLibpow: \t{0}", small_ifbspow);
- Console.WriteLine("Small\tCtbLibpow: \t{0}", small_ctblpow);
- Console.WriteLine("Big\tPurepow: \t{0}", big_pure);
- Console.WriteLine("Big\tLibpow: \t{0}", big_libpow);
- Console.WriteLine("Big\tIfLibpow: \t{0}", big_iflibpow);
- Console.WriteLine("Big\tIfbsLibpow: \t{0}", big_ifbspow);
- Console.WriteLine("Big\tCtbLibpow: \t{0}", big_ctblpow);
- Console.ReadKey(true);
- }
- static double LibPow(double x, int n)
- {
- if (n < 0)
- { return (1.0 / LibPow(x, -n)); }
- switch (n)
- {
- case 0:
- { return (1.0); }
- case 1:
- { return (x); }
- case 2:
- { return (x * x); }
- case 3:
- { return (x * x * x); }
- case 4:
- {
- double x2 = x * x;
- return (x2 * x2);
- } case 5:
- {
- double x2 = x * x;
- return (x2 * x2 * x);
- }
- case 6:
- {
- double x2 = x * x;
- return (x2 * x2 * x2);
- }
- case 7:
- {
- double x3 = x * x * x;
- return (x3 * x3 * x);
- }
- case 8:
- {
- double x2 = x * x;
- double x4 = x2 * x2;
- return (x4 * x4);
- }
- case 9:
- {
- double x3 = x * x * x;
- return (x3 * x3 * x3);
- }
- case 10:
- {
- double x2 = x * x;
- double x4 = x2 * x2;
- return (x4 * x4 * x2);
- }
- default:
- { return (Math.Pow(x, n)); }
- }
- }
- static double IfLibPow(double x, int n)
- {
- if (n < 0)
- { return (1.0 / IfLibPow(x, -n)); }
- if (n == 0)
- { return (1.0); }
- else if (n == 1)
- { return (x); }
- else if (n == 2)
- { return (x * x); }
- else if (n == 3)
- { return (x * x * x); }
- else if (n == 4)
- {
- double x2 = x * x;
- return (x2 * x2);
- }
- else if (n == 5)
- {
- double x2 = x * x;
- return (x2 * x2 * x);
- }
- else if (n == 6)
- {
- double x2 = x * x;
- return (x2 * x2 * x2);
- }
- else if (n == 7)
- {
- double x3 = x * x * x;
- return (x3 * x3 * x);
- }
- else if (n == 8)
- {
- double x2 = x * x;
- double x4 = x2 * x2;
- return (x4 * x4);
- }
- else if (n == 9)
- {
- double x3 = x * x * x;
- return (x3 * x3 * x3);
- }
- else if (n == 10)
- {
- double x2 = x * x;
- double x4 = x2 * x2;
- return (x4 * x4 * x2);
- }
- else
- return (Math.Pow(x, n));
- }
- static double IfBsearchLibPow(double x, int n)
- {
- if (n <= 5)
- if (n <= 1)
- if (n < 0)
- return (1.0 / IfBsearchLibPow(x, -n)); // < 0
- else
- if (n == 0)
- return 1; // 0
- else
- return x; // 1
- else
- if (n <= 3)
- if (n == 2)
- return (x * x); // 2
- else
- return (x * x * x); // 3
- else
- if (n == 4)
- {
- double x2 = x * x;
- return (x2 * x2);
- }
- else // 5
- {
- double x2 = x * x;
- return (x2 * x2 * x);
- }
- else
- if (n <= 9)
- if (n <= 7)
- if (n == 6) // 6
- {
- double x2 = x * x;
- return (x2 * x2 * x2);
- }
- else // 7
- {
- double x3 = x * x * x;
- return (x3 * x3 * x);
- }
- else
- if (n == 8) // 8
- {
- double x2 = x * x;
- double x4 = x2 * x2;
- return (x4 * x4);
- }
- else // 9
- {
- double x3 = x * x * x;
- return (x3 * x3 * x3);
- }
- else
- if (n == 10) // 10
- {
- double x2 = x * x;
- double x4 = x2 * x2;
- return (x4 * x4 * x2);
- }
- else // > 10
- return Math.Pow(x, n);
- }
- static double CtblLibPow(double x, int n)
- {
- if (n < 0)
- { return (1.0 / CtblLibPow(x, -n)); }
- if (n > 10)
- { return Math.Pow(x, n); }
- return Ctbl[n](x);
- }
- static void InitCtbl()
- {
- Ctbl = new List<Func<double, double>>();
- Ctbl.Add((x) => 1);
- Ctbl.Add((x) => x);
- Ctbl.Add((x) =>
- { return (x * x); });
- Ctbl.Add((x) =>
- { return (x * x * x); });
- Ctbl.Add((x) =>
- {
- double x2 = x * x;
- return (x2 * x2);
- });
- Ctbl.Add((x) =>
- {
- double x2 = x * x;
- return (x2 * x2 * x);
- });
- Ctbl.Add((x) =>
- {
- double x2 = x * x;
- return (x2 * x2 * x2);
- });
- Ctbl.Add((x) =>
- {
- double x3 = x * x * x;
- return (x3 * x3 * x);
- });
- Ctbl.Add((x) =>
- {
- double x2 = x * x;
- double x4 = x2 * x2;
- return (x4 * x4);
- });
- Ctbl.Add((x) =>
- {
- double x3 = x * x * x;
- return (x3 * x3 * x3);
- });
- Ctbl.Add((x) =>
- {
- double x2 = x * x;
- double x4 = x2 * x2;
- return (x4 * x4 * x2);
- });
- }
- static List<Func<double, double>> Ctbl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement