Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Test Results</h2> <p>10,000,000 iterations of <strong>Block 1</strong></p> <pre><code>myFlag = 0: 23.8ns per iteration myFlag = 1: 23.8ns per iteration </code></pre> <p>10,000,000 iterations of <strong>Block 2</strong></p> <pre><code>myFlag = 0: 23.8ns per iteration myFlag = 1: 46.8ns per iteration </code></pre> <p><strong>Block 2</strong> is 96% slower than <strong>Block 1</strong>. Makes sense, since <strong>Block 2</strong> does twice the work in the pessimistic case.</p> <blockquote> <p>i prefer either case, depending on the situation. If <code>myFlag</code> is <em>rarely ever</em> 1, then it want it to stand out as the edge case that we have to handle. If both are equally likely, i want the <code>if-else</code> syntax. But that's preference, not fact.</p> </blockquote> <hr> <p>Decades ago, the intel 80286 dual pipeline would stall if a conditional jump was taken, rather than falling through to the next instruction. By the time of the Pentium that went away; the CPU pre-fetches both branch paths. But in the back of my mind i still have a twinge of fear whenever i write code that has the most common outcome in the <code>else</code> clause. Every time i have to remind myself that it doesn't matter anymore.</p> <hr> <pre><code>Int32 reps = 10000000; private void Block1(int myFlag) { String width; String height; Stopwatch sw = new Stopwatch(); sw.Start(); for (int i = 0; i &lt; reps; i++) { if (myFlag == 1) { width = String.Format("{0:g}%", 60); height = String.Format("{0:g}%", 60); } else { width = String.Format("{0:g}%", 80); height = String.Format("{0:g}%", 80); } } sw.Stop(); Double time = (Double)sw.Elapsed.Ticks / Stopwatch.Frequency * 1000000000.0 / reps; MessageBox.Show(time.ToString() + " ns"); } private void Block2(int myFlag) { String width; String height; Stopwatch sw = new Stopwatch(); sw.Start(); for (int i = 0; i &lt; reps; i++) { width = String.Format("{0:g}%", 80); height = String.Format("{0:g}%", 80); if (myFlag == 1) { width = String.Format("{0:g}%", 60); height = String.Format("{0:g}%", 60); } } sw.Stop(); Double time = (Double)sw.Elapsed.Ticks / Stopwatch.Frequency * 1000000000.0 / reps; MessageBox.Show(time.ToString() + " ns"); } </code></pre> <ul> <li><code>String.Format</code> makes <code>IF</code> 96% slower</li> <li><code>GetPercentageString(0.60)</code> makes <code>IF</code> 96% slower</li> </ul> <hr> <pre><code>const reps = 10000000; procedure Block1(myflag: Integer); var width, height: string; i: Integer; t1, t2: Int64; time: Extended; freq: Int64; begin QueryPerformanceCounter(t1); for i := 1 to reps do begin if myFlag = 1 then begin width := '60%'; height := '60%'; end else begin width := '80%'; height := '80%'; end; end; QueryPerformanceCounter(t2); QueryPerformanceFrequency(freq); time := (t2-t1) / freq * 1000000000 / reps; ShowMessage(FloatToStr(time)+ 'ns'); end; procedure Block2(myflag: Integer); var width, height: string; i: Integer; t1, t2: Int64; time: Extended; freq: Int64; begin QueryPerformanceCounter(t1); for i := 1 to reps do begin width := '80%'; height := '80%'; if myFlag = 1 then begin width := '60%'; height := '60%'; end; end; QueryPerformanceCounter(t2); QueryPerformanceFrequency(freq); time := (t2-t1) / freq * 1000000000 / reps; ShowMessage(FloatToStr(time)+ 'ns'); end; </code></pre> <hr> <p>Doing twice the amount of work takes roughly twice the amount of time.</p> <p>Answer: IF does not perform better than IF-ELSE.</p> <hr> <p><img src="https://i.stack.imgur.com/CCQRH.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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