Just a swift expansion of pswg's Benchmark contribution -
I wanted to see another comparison, equivalent to 10 ^ x ==> e ^ (x * ln (10)) or {double ln10 = Math.Log(10.0); y = Math.Exp(x * ln10);} {double ln10 = Math.Log(10.0); y = Math.Exp(x * ln10);}
Here is what I have:
Operation Time Math.Exp(x) 180 ns (nanoseconds) Math.Pow(y, x) 440 ns Math.Exp(x*ln10) 160 ns Times are per 10x calls to Math functions.
I donβt understand why the time to turn on multiplication in the loop before entering Exp() sequentially leads to a reduction in time if there is no error in this code or the algorithm depends on the value?
The program follows.
namespace _10X { public partial class Form1 : Form { int nLoops = 1000000; int ix; // Values - Just to not always use the same number, and to confirm values. double[] x = { 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5 }; public Form1() { InitializeComponent(); Proc(); } void Proc() { double y; long t0; double t1, t2, t3; t0 = DateTime.Now.Ticks; for (int i = 0; i < nLoops; i++) { for (ix = 0; ix < x.Length; ix++) y = Math.Exp(x[ix]); } t1 = (double)(DateTime.Now.Ticks - t0) * 1e-7 / (double)nLoops; t0 = DateTime.Now.Ticks; for (int i = 0; i < nLoops; i++) { for (ix = 0; ix < x.Length; ix++) y = Math.Pow(10.0, x[ix]); } t2 = (double)(DateTime.Now.Ticks - t0) * 1e-7 / (double)nLoops; double ln10 = Math.Log(10.0); t0 = DateTime.Now.Ticks; for (int i = 0; i < nLoops; i++) { for (ix = 0; ix < x.Length; ix++) y = Math.Exp(x[ix] * ln10); } t3 = (double)(DateTime.Now.Ticks - t0) * 1e-7 / (double)nLoops; textBox1.Text = "t1 = " + t1.ToString("F8") + "\r\nt2 = " + t2.ToString("F8") + "\r\nt3 = " + t3.ToString("F8"); } private void btnGo_Click(object sender, EventArgs e) { textBox1.Clear(); Proc(); } } }
So, I think I go with Math.Exp(x * ln10) until someone finds an error ...
source share