|
The second one is faster I think (with a couple of µs that is)
The first one is far, far better.
V.
I found a living worth working for, but haven't found work worth living for.
|
|
|
|
|
Any improvement in speed the second may have pales in comparison to the cost in maintainability.
Any why have both values be strings?
And why have a DropDownList simply to select a number? Use a NumericUpDown or a TextBox with validation.
I really dislike Websites that have a list of (U.S.) state abbreviations or whatnot when it's simpler just to type in the data... e.g. I now live in Arizona, but rather than allowing me to enter "AZ" (two keystrokes) I need to either scroll down or press "A" five times! Just so the programmer doesn't have to validate the entry, pathetic.
|
|
|
|
|
sylvesterg wrote: which one is faster
It is easy to check: just make a loop with a couple of million items to add, and then type the second version up to the same number, and you'll be able to measure it with a stopwatch.
|
|
|
|
|
The best is use make an array with the strings and after use items.AddRange, and also for Painting part.
|
|
|
|
|
I'd go with the first as being better. Besides, the overhead in "DropDownList1.Items.Add()" itself is enough to mask any possible speed difference between the two constructs. Really, both should execute fast enough that you won't notice it anyway. Speed diff can be so easily lost due to random interrupts and kernel scheduling variations that it becomes a "who cares" issue for the most part.
And the explicit one... what a nightmare to maintain.
|
|
|
|
|
The first is the better because it is more readable and bug-safer. In the second you have to paste and copy too much.
I would write
String s;//here
for(int i=1;i<=100;i++)
{
const String s = i.ToString();//could be best, so let the compiler do the optimzing !!!
DropDownList1.Items.Add(new ListItem(s,s));
}
so I win one call
The first one is the best for the programmer - so what second
Greetings from Germany
|
|
|
|
|
Would it be better to do the call to i.ToString() only once and assign it to a variable?
Being in a minority of one, doesn't make you insane George Orwell However, in my case it does
|
|
|
|
|
The latter method is somewhat like C++ inline qualifier for function right?
|
|
|
|
|
First, you are not supposed to ask questions here.
Now, on modern day machines it does not matter that much. The second would be faster, at least it use to be and is called unrolling. In the old days the difference would be noticeable by the user and is why unrolling loops was sometime needed, especially in graphics applications. In this case the new operation is more of a bottle neck than the loop, but pre-allocating memory space can help solve that problem.
Any way,
Just use the loop form, as it is easier to maintain and you probably do not have a legitimate reason not to.
INTP
"Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra
|
|
|
|
|
Recently a contractor(my View about Contractors - are supposed to good programmers!) worked in our project.
Code written in C#-VS2005
He wrote
<br />
public void RegisterEvents()<br />
{<br />
obj.CenterChanged += new CenterChangedEventHandler(OnCenterChanged);<br />
obj.CenterChanged += new CenterChangedEventHandler(OnCenterChanged);<br />
}<br />
My question:
Would registering the same event twice call the handler twice when event is raised?
-rt
|
|
|
|
|
I did a quick check myself and indeed the handler gets called twice if registered twice.
The handler handled some drawing code which I am sure would redraw twice!
|
|
|
|
|
Contractors aren't magic bullets for organisations. Like every other profession, some are good and some are bad. Remember that a lot of people are attracted to contracting for the perceived high remuneration and you can see why there is a lot of chaff among the wheat.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
|
Yeah it should get called twice
I find a number of contractors are hackers because they just code to the contract with no thought to maintainability.
I'm sure there are some good ones out there, but they are much more immediate result oriented, which can lead to bad code. I've seen it a few times, not to say permanent coders can't be hackers too.
Tim
|
|
|
|
|
True. Their sense of commitment to the organization is less since they know they would escape after a while.
We need to be careful in getting the proper deliverables from them since our project is billed by them.
|
|
|
|
|
Yes.
But wasn't there a compiler warning or at least a peer code review to stamp out this?
|
|
|
|
|
Seen in production code
int intTrStatusCnt = 0;
long intMaxTrLogID = 0;
long intMaxTrStatusID = 0;
Cheers,
Vıkram.
Déjà moo - The feeling that you've seen this bull before.
Join the CP group at NationStates. Password: byalmightybob
|
|
|
|
|
That's definitely not Hungarian believe me :P
|
|
|
|
|
szukuro wrote: That's definitely not Hungarian believe me
... hence the phrase 'use it properly'.
Cheers,
Vıkram.
Déjà moo - The feeling that you've seen this bull before.
Join the CP group at NationStates. Password: byalmightybob
|
|
|
|
|
OK, I should've used the joke icon. If you take a look at my profile, I'm from Hungary. The joke was based upon the fact that Hungarian is not the same as Hungarian Notation.
|
|
|
|
|
Touché!
Cheers,
Vıkram.
Déjà moo - The feeling that you've seen this bull before.
Join the CP group at NationStates. Password: byalmightybob
|
|
|
|
|
I got it. =)
I knew right away you had to be from Hungary, with that sense of humor, haha.
|
|
|
|
|
And neither Italian, I can assure it!
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
The Grand Negus wrote: Perhaps the guy changed the latter two types after the thing was running to fix a bug
Revision 1.0.
Cheers,
Vıkram.
Déjà moo - The feeling that you've seen this bull before.
Join the CP group at NationStates. Password: byalmightybob
|
|
|
|
|
Perhaps the guy was using his own convention. If he used it consistently, I don't think he is to blame.
Sorry I haven't noticed the long vars!!!
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|