|
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 (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
|
|
|
|
|
That's what I though would happen. What I think the comiler did to your original code, was that since your using contant numbers, the "?:" statement was optimized into the only result possible for that statement and was replaced by a simple numerical assignment.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
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);
Console.WriteLine(DateTime.Now.ToString());
timer.Start();
for (int IterationsRemaining = 1000000000; IterationsRemaining >= 0; IterationsRemaining--)
{
Num1 = RNG.Next();
Num2 = RNG.Next();
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
|
|
|
|
|
|
In fact, I tried the aforesaid code with the floating point numbers also, by replacing the first lines of the code (above) with the following sample code:
float minValue;
float val1 = 5.3214F;
float val2 = 7.7564F;
The result is still the same. The second method (i.e., the if-else method) is almost 100% faster than the first method (i.e., the Static Math method). The practical results on my machine were 17 seconds and 8 seconds respectively for 10 billion iterations for the said float numbers for the aforesaid two methods.
Regards
|
|
|
|
|
Without further investigation the second one would seem faster. If you really want to know for sure construct a simple test example that just reads each pixel on a bitmap and runs one or the other comparison and time the event.
However I would caution you about attempting optimization like this because premature optimization is evil. Without profiling tools trying to optmize can lead to confusing code and worse even worse performance (ie. producing bogus output, crashing, etc). The best optimization might happen outside of this code and quite possibly be in another part of the loop. Or ultimately, this is a "hardware bound" problem where no matter how much tweaking you do to code you are better off taking the amount of money they would have paid you to tweak code and buying new hardware with it.
|
|
|
|
|
Hi.I need an algorithm in C# or any other language for 2Dimensional Cutting Stock Problem.Please help me cause I m very worried about the result of my work.
|
|
|
|
|
Hi...
I have file with map in MapInfo format... How can i read this file using C#...
and how can i draw a map on form?
Greetings
Yarns
|
|
|
|
|
Can anyone guide me how to create auto hide feature.or can anyone give me sample code to create auto hide fucntionality in C# or in VB.NET.
Umair
|
|
|
|
|
If I understand correctly, what you want to know about is a window which hides automatically when the focus is moved away from it (for example certain windows in the IDE of Visual Studio .NET). If it is so, you may try to have a look at the source code here[^]), though you may have to go through tons of code.
|
|
|
|
|
Yes you understand correct,i need same effect.But i have found my own way,its quites simple and quites easy.Thx anyways
Umair
|
|
|
|
|
how did you do? Can you explain.
|
|
|
|
|
Is it possible to paint over the top of a child component without overriding the OnPaint function for the child component?
|
|
|
|
|
|
No, I am attempting to paint a grid in the forms OnPaint function but can't find a way of painting on top of the child controls.
|
|
|
|
|
Hi,
I want to be able to view pdf files in Winforms. The easiest way I could think of was to create a webcontrol in my winforms. I knew you could very easely do this in C++, but in C# I didn't find such a control. Looking on MSDN and CP I found things like WebClient and WebControl classes, but I can't figure out how they work, in what control do you view the actual page?
So there would be two possible ways I could think of:
1: Create a WebControl
2: Embed I-Explorer into the application.
Can anyone give me any advise what would be best and to what I should look for?
tnx!
No hurries, no worries.
[EDIT]
I found it, off course it was easy enough .
Choose add user control in the toolbox, under COM component choose Microsoft Web Control.
Add the webcontrol to your from.
That's it.
[/EDIT]
|
|
|
|
|
use axWebBrowser control, it is able to view pdf files and also other MS Office file, it is present on the left side panel,DATA panel or add it.
sample is like this
object empty = System.Reflection.Missing.Value;
webBroswerControl.Navigate("c:\\abc.pdg", ref empty, ref empty, ref empty, ref empty);
i hope it will help u
|
|
|
|
|
tnx! I found it before your answer, it was easy enough when I did find it, you only have to stumble upon it .
No hurries, no worries.
|
|
|
|
|
Hi,
you can use axWebBrowser control and call navigate method.
Thanks and Regards
SGS
|
|
|
|
|
tnx! I found it before your answer, it was easy enough when I did find it, you only have to stumble upon it .
No hurries, no worries.
|
|
|
|
|
Hello C# Professionals,
I really need to help of you..
I have a datagrid on my winform and I would like to open this datagrid as READ-ONLY...and then I would like to enable (write-enabled) a specific row with a button click..
I have written a code to select entire row in the datagrid.Now
----------------------------------------------------
private void grdPhone_CurrentCellChanged(object sender, System.EventArgs e)
{
this.grdPhone.Controls.Clear();
this.grdPhone.Select(this.grdPhone.CurrentCell.RowNumber);
}
-----------------------------------------------------
Now what will I do in the button_click event..??
Please help
|
|
|
|
|
Hello dear buddies,
I have read System.Reflection types that help to dynamically invoke object members are slow and are not suitable for high-performance applications.
My question:
Is the low speed because of the member search that is performed to find the requested member to get respectful 'MemberInfo' object (to invoke it) OR not only the member name search but also the member invokation using 'MemberInfo' object is a slow process itself?
A comparision between speed of direct method call and dynamic method call using 'MethodInfo.Invoke()' really helps me to find my answer too.
Thank you so much for any help and note
- Den
---
"Art happens when you least expect it."
|
|
|
|
|
Hi
iam tryin ro make programe about robot simiulation
please tell me how can i move and rotate objects ?
also i want to use pixels how can i do it?
tanks for your help and regards
|
|
|
|
|
That's a really generic question and way to large to be answered in the forums.
It doesn't sound like you have enough experience in the MANY different fields your going to need to accomplish this. I'd suggest picking up books having to do with DirectX, 3D modeling, and animation.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
is it possible to hide/disable console window at the time of calling some console command/utility from C#,for instance,when i call "DIR" command within C#,it first popsup console window for few seconds and then outputs data in the variable or file,whatever has been chosen
thanks
MyBlogs
http://weblogs.com.pk/kadnan
|
|
|
|