Note that there are some explanatory texts on larger screens.

plurals
  1. POSimple math operations faster on double than on float datatype?
    text
    copied!<blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/158889/are-doubles-faster-than-floats-in-c">Are doubles faster than floats in c#?</a> </p> </blockquote> <p>I wrote simple benchmark to check how much performance i can get changing <code>double</code> datatype to <code>float</code> in my application. Here is my code:</p> <pre><code> // my form: // one textbox: textbox1 (MultiLine property set to true) // one button: button1 with event button1_Click private void button1_Click(object sender, EventArgs e) { int num = 10000000; float[] floats1 = new float[num]; float[] floats2 = new float[num]; float[] floatsr = new float[num]; // array for results double[] doubles1 = new double[num]; double[] doubles2 = new double[num]; double[] doublesr = new double[num]; // array for results Stopwatch stw = new Stopwatch(); log("Preparing data"); Random rnd = new Random(); stw.Start(); for (int i = 0; i &lt; num; i++) { floats1[i] = NextFloat(rnd); floats2[i] = NextFloat(rnd); doubles1[i] = rnd.NextDouble(); doubles2[i] = rnd.NextDouble(); } stw.Stop(); log(stw.Elapsed.TotalMilliseconds.ToString()+"ms"); stw.Reset(); log(""); stw.Start(); for (int i = 0; i &lt;# i++) { floatsr[i] = floats1[i] * floats2[i]; } stw.Stop(); log("Multiplying floats: " + stw.Elapsed.TotalMilliseconds.ToString() + "ms"); stw.Reset(); stw.Start(); for (int i = 0; i &lt; num; i++) { doublesr[i] = doubles1[i] * doubles2[i]; } stw.Stop(); log("Multiplying doubles: " + stw.Elapsed.TotalMilliseconds.ToString() + "ms"); stw.Reset(); stw.Start(); for (int i = 0; i &lt; num; i++) { floatsr[i] = floats1[i] / floats2[i]; } stw.Stop(); log("Dividing floats: " + stw.Elapsed.TotalMilliseconds.ToString() + "ms"); stw.Reset(); stw.Start(); for (int i = 0; i &lt; num; i++) { doublesr[i] = doubles1[i] / doubles2[i]; } stw.Stop(); log("Dividing doubles: " + stw.Elapsed.TotalMilliseconds.ToString() + "ms"); stw.Reset(); } private void log(string text) { textBox1.Text = textBox1.Text + text + Environment.NewLine; } // I found that function somewhere on stackoverflow static float NextFloat(Random random) { double mantissa = (random.NextDouble() * 2.0) - 1.0; double exponent = Math.Pow(2.0, random.Next(-126, 128)); return (float)(mantissa * exponent); } </code></pre> <p>I got results like this (release, no debug, Intel Mobile Core Duo T2500 2.0GHz 2MB CPU):</p> <pre><code>Preparing data 5275,6862ms Multiplying floats: 442,7865ms Multiplying doubles: 169,4028ms Dividing floats: 550,7052ms Dividing doubles: 164,1607ms </code></pre> <p>I was suprised, that operations on <code>double</code> are almost 3 times faster than operations on <code>float</code>. I searched for "double float" here, and i found this:</p> <p><a href="https://stackoverflow.com/questions/3426165/is-using-double-faster-than-float">Is using double faster than float?</a></p> <p>Best answer is focused on CPU architecture, but I cant agree with that.</p> <p>I suspect that something else is causing low performance on floats, because my CPU with Intel SSE should be able to multiply or divide 4 floats at once (packed floating point instructions), or 2 doubles at once. So floats should be faster.</p> <p>Maybe compiler (or clr in .net) is optimizing memory usage somehow?</p> <p><strong>Is there any way to optimize it and make float faster?</strong></p> <p><strong>Please don't report duplicate, i saw other questions and they not satisfying me.</strong></p> <hr> <p>My results after changing method for generating floats now look fine (suggested by Servy): </p> <pre><code>Preparing data 1367,0678ms Multiplying floats: 109,8742ms Multiplying doubles: 149,9555ms Dividing floats: 167,0079ms Dividing doubles: 168,6821ms </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload