Click here to Skip to main content
15,897,273 members
Home / Discussions / C#
   

C#

 
AnswerRe: Which one is faster - Static function calls or If else? Pin
leppie21-Apr-05 0:51
leppie21-Apr-05 0:51 
GeneralRe: Which one is faster - Static function calls or If else? Pin
abcxyz8221-Apr-05 1:08
abcxyz8221-Apr-05 1:08 
GeneralRe: Which one is faster - Static function calls or If else? Pin
leppie21-Apr-05 1:39
leppie21-Apr-05 1:39 
AnswerRe: Which one is faster - Static function calls or If else? Pin
Ashok Dhamija21-Apr-05 1:50
Ashok Dhamija21-Apr-05 1:50 
GeneralRe: Which one is faster - Static function calls or If else? Pin
Dave Kreskowiak21-Apr-05 2:38
mveDave Kreskowiak21-Apr-05 2:38 
GeneralRe: Which one is faster - Static function calls or If else? Pin
Ashok Dhamija21-Apr-05 18:57
Ashok Dhamija21-Apr-05 18:57 
GeneralRe: Which one is faster - Static function calls or If else? Pin
Dave Kreskowiak22-Apr-05 1:11
mveDave Kreskowiak22-Apr-05 1:11 
GeneralRe: Which one is faster - Static function calls or If else? Pin
Dave Kreskowiak22-Apr-05 9:16
mveDave Kreskowiak22-Apr-05 9:16 
Your code was slowed down because you kept killing and recreating a new RNG with every number you generated and used limits on the range of numbers returned. You also have the extra overhead of making a method call to create a new RNG, seed it, and return a number. This wasn't necessary since we weren't testing the performance of the RNG functions.

If you want wanted to compare the two methods on the exact same stream of numbers (and do it quickly!), then you have to create an RNG with a known seed value and retain it throughout the entire test and minimize the number of calculations that it has to do to return a number.

I ran similar code to yours, running 1 Billion iterations in just under one minute:
int Num1, Num2, Num3;
Stopwatch timer = new Stopwatch();
Random RNG = new Random(3456);      // Known seed value
Console.WriteLine(DateTime.Now.ToString());
 
timer.Start();
for (int IterationsRemaining = 1000000000; IterationsRemaining >= 0; IterationsRemaining--)
{
    Num1 = RNG.Next();         // Who cares what the limits are.  Just return
    Num2 = RNG.Next();         // the next 32-bit integer in the list of random numbers.
    Num3 = (Num1 > Num2) ? Num2 : Num1;
}
timer.Stop();
 
Console.WriteLine(DateTime.Now.ToString());
Console.WriteLine(timer.Elapsed.ToString());

Using the known seed value, rerunning this code with the only change being the Num3= line, the RNG will generate the exact same string of numbers.

The results I got on a Dell GX270, C# 2005, running 3 passes of 1 Billion iterations each:
Release compiled code:  (int types)
       Math.Min:  46.11    47.02    45.92 seconds  (Avg:  46.35 seconds)
    ?: operator:  39.78    40.04    39.37 seconds  (Avg:  39.73 seconds)

Here's something interesting:
Release compiled code:  (double types)
       Math.Min:  114.10  113.66   111.77 seconds  (Avg:  113.17 seconds)
    ?: operator:   56.91   56.95    56.97 seconds  (Avg:   56.94 seconds)



RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

GeneralRe: Which one is faster - Static function calls or If else? Pin
leppie21-Apr-05 6:18
leppie21-Apr-05 6:18 
GeneralRe: Which one is faster - Static function calls or If else? Pin
Ashok Dhamija21-Apr-05 18:19
Ashok Dhamija21-Apr-05 18:19 
AnswerRe: Which one is faster - Static function calls or If else? Pin
Tom Larsen21-Apr-05 5:23
Tom Larsen21-Apr-05 5:23 
GeneralPlease help.... Pin
KORCARI21-Apr-05 0:42
KORCARI21-Apr-05 0:42 
GeneralMapInfo files... drawing map... Pin
yarns21-Apr-05 0:07
yarns21-Apr-05 0:07 
GeneralHelp -- AutoHide panels Pin
Umair Tariq20-Apr-05 22:45
Umair Tariq20-Apr-05 22:45 
GeneralRe: Help -- AutoHide panels Pin
Ashok Dhamija21-Apr-05 2:10
Ashok Dhamija21-Apr-05 2:10 
GeneralRe: Help -- AutoHide panels[Solved] Pin
Umair Tariq28-Apr-05 20:27
Umair Tariq28-Apr-05 20:27 
GeneralRe: Help -- AutoHide panels[Solved] Pin
erdem0122-Dec-08 3:20
erdem0122-Dec-08 3:20 
GeneralPainting Pin
Mark_Harrison20-Apr-05 22:16
Mark_Harrison20-Apr-05 22:16 
GeneralRe: Painting Pin
S. Senthil Kumar20-Apr-05 23:08
S. Senthil Kumar20-Apr-05 23:08 
GeneralRe: Painting Pin
Mark_Harrison20-Apr-05 23:40
Mark_Harrison20-Apr-05 23:40 
GeneralWebpages in Winforms[Solved] Pin
V.20-Apr-05 22:00
professionalV.20-Apr-05 22:00 
GeneralRe: Webpages in Winforms Pin
Umair Tariq20-Apr-05 22:49
Umair Tariq20-Apr-05 22:49 
GeneralRe: Webpages in Winforms Pin
V.20-Apr-05 23:13
professionalV.20-Apr-05 23:13 
GeneralRe: Webpages in Winforms[Solved] Pin
Kodanda Pani20-Apr-05 23:08
Kodanda Pani20-Apr-05 23:08 
GeneralRe: Webpages in Winforms[Solved] Pin
V.20-Apr-05 23:11
professionalV.20-Apr-05 23:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.