Click here to Skip to main content
15,900,258 members
Home / Discussions / C#
   

C#

 
GeneralMethodInfo.Invoke urgent help please Pin
hasansheik21-Apr-05 1:13
hasansheik21-Apr-05 1:13 
QuestionWhich one is faster - Static function calls or If else? Pin
abcxyz8221-Apr-05 0:47
abcxyz8221-Apr-05 0:47 
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 
Yes, I compared the performance of the said two methods by using random numbers. Here is the code which I used (this time I used only 1 million iterations instead of 10 billion iterations because of time-consuming random number generation process):
private void button1_Click(object sender, System.EventArgs e)
		{					
			DateTime time1 = DateTime.Now;
			for(long i=0; i<1000000; i++)
			{
				int val1 = Random1To99(); 
				int val2 = Random1To99();
				int minValue = Math.Min ( val1, val2 );
			}
			DateTime time2 = DateTime.Now;
			TimeSpan diff1 = time2 - time1;
			label1.Text = diff1.Seconds.ToString();
		}

		private void button2_Click(object sender, System.EventArgs e)
		{			
			DateTime time1 = DateTime.Now;
			for(long i=0; i<1000000; i++)
			{				
				int val1 = Random1To99(); 
				int val2 = Random1To99();
				int minValue = val1 < val2 ? val1 : val2;
			}
			DateTime time2 = DateTime.Now;
			TimeSpan diff1 = time2 - time1;
			label2.Text = diff1.Seconds.ToString();
		}

And, the method to generate the random numbers is as under (for the sake of simplicity, this method generates random numbers in the range of 1 to 99 only; secondly, I declared mySeed variable at the class level to try to get the "real" random numbers by using a real different seed every time):
private int mySeed = 0;
		private int Random1To99()
		{
			DateTime dt = DateTime.Now;
			mySeed += (int)(dt.Millisecond);
			Random rnd = new Random(mySeed);
			//return a number in the range of 1 to 99
			return (int) rnd.Next(1, 100);
		}

This time, both the methods are taking the same time of 37 seconds on my machine. The result is the same even though I tried it a few times.

The reason appears to be that the generation of one random number takes perhaps 1000 to 10000 times more time than either of the methods being compared! And, then use this for 1 million times!! In the end, what one finds is that out of 37 seconds taken for the aforesaid operation, perhaps 99.999% of the processing time is taken only for the random number generations and the remaining appx. 0.0001% time is being compared for the said two methods. So, expectedly, we get the same time of about 37 seconds for the above two methods.

To conclude, what I personally feel is that once it is accepted that the second method of if-else takes less time for any given combination of two numbers as compared to the first method of Math static function, then this result would broadly be the same even for any other combination of randomly selected numbers.

Regards
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 
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 

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.