|
Don't sweat the small stuff. I never worry about Garbage Collection; that's the point of having it.
I prefer to use for when I can, but I'm old-school that way.
Super Lloyd wrote: nested foreach
I don't think I've ever needed that.
You'll never get very far if all you do is follow instructions.
|
|
|
|
|
I am not too worried..
But I am writing custom Panel (for WPF) which will be in hierarchical resizable items controls...
And they will do multi pass layout too...
Hence I am going to do a lot of recursive call on every mouse move events...
So far it's very quick and I am not really worried!
However I can't help to see how, in the .NET source code, they use struct as much as possible in such case and even use an internal BooleanBoxes to optimize boxing away for DependencyProperty(ies)...
And I though if I could reduce boxing and memory allocation in my panels, that would be nice too!
And I noticed the foreach => GetEnumerator() and was wondering about it!
It's not micro optimization, as this is a process on its own which has no link with anything else. But it will be a process that will be called regularly, why not spend 1 more hour to make it better?
At any rate.. I am using for loop now and only struct in my layout code and I reuse the same array, when I need one!
So memory wise it's good enough!
|
|
|
|
|
I think I'd be more concerned about the recursion.
You'll never get very far if all you do is follow instructions.
|
|
|
|
|
Well, it can't be helped!
RibbonBars contains RibbonGroups which contains RibbonItems which contains Header!
(And MenuItems contains sub MenuItems which contains Header too)
Yeah I am making my own Ribbon library! But wait, there is a twist, it's called... MenuRibbon! And most ItemsControl in it have no particular container type! :P
|
|
|
|
|
Use the for loop .
I rarely use the foreach, because despite your personal feeling of being more readable, I find it less readable. (especially in the sense of what it is doing).
|
|
|
|
|
Using it now!
BTW, in some structure (say linked list for example) it is not really practical to use the for loop!
|
|
|
|
|
I know. I occasionally use the foreach too
|
|
|
|
|
Foreach allocates an enumerator and calls MoveNext and Current multiple times. For allocates a register variable (or whatever those are called in .Net) and also calls the property accessor Count (and, presumably, the indexer inside the loop) multiple times. The memory hit from the class reference against an integer variable is so small you shouldn't care, and the execution time will be very similar.
|
|
|
|
|
Super Lloyd wrote: So, a couple of nested foreach, bang, allocation, then GC, then slow down!
Presumably you know this because you actually measured it using realistic (production) data using a realistic (production) scenario.
If that isn't true then this exercise is pointless.
If it is true then the first and very best way to increase performance is to attempt to re-evaluate the algorithm itself. Thus instead of trying to make for loop faster try to eliminate all the for loops entirely.
|
|
|
|
|
I totally agree with this, what you're attempting sounds like premature optimisation and doing so without knowing the true bottle necks is usually a great big waste of time.
|
|
|
|
|
Hi,
I am using the following for() to loop through a datagrid to save to a database. My problem here is: when looping, I am getting all records correct except the last record which I am getting a blank cell value for it. what's wrong with my for() please?
for (int i = 0; i <= gridContacts.RowCount; i++)
{
sql_command = new MySqlCommand("sp_add_new_employee_contact", sql_connection);
sql_command.CommandType = CommandType.StoredProcedure;
sql_command.CommandTimeout = Convert.ToInt32(string_encryptor.DecryptString(xmlClass.read_xml_value("HRMS\\HRMS", "CommandTimeOut"), "JassimRahma@731004167"));
sql_command.Parameters.AddWithValue("param_employee_id", employee_id).MySqlDbType = MySqlDbType.Int32;
sql_command.Parameters.AddWithValue("param_contact_category", Convert.ToInt32(gridContacts.GetRowCellValue(i, "contact_category"))).MySqlDbType = MySqlDbType.Int32;
sql_command.Parameters.AddWithValue("param_contact_details", Convert.ToString(gridContacts.GetRowCellValue(i, "contact_details"))).MySqlDbType = MySqlDbType.VarChar;
sql_command.Parameters.AddWithValue("param_contact_description", Convert.ToString(gridContacts.GetRowCellValue(i, "contact_description"))).MySqlDbType = MySqlDbType.VarChar;
MessageBox.Show(Convert.ToString(gridContacts.GetRowCellValue(i, "contact_details")));
int result_rows = sql_command.ExecuteNonQuery();
}
Thanks,
Jassim
Technology News @ www.JassimRahma.com
|
|
|
|
|
Maybe
i < gridContacts.RowCount
?
Cheers
|
|
|
|
|
Why are you recreating the same MySqlCommand and parameters objects every trip through the loop? Wouldn't it better to just create them ONCE, before the loop, and then just populate the parameters and execute the command inside the loop?
|
|
|
|
|
Hear! Hear!
You'll never get very far if all you do is follow instructions.
|
|
|
|
|
There is a lot of bad in that code. Others have pointed out some.
But also...
0) Don't bother trying to set the MySqlDbType of the Parameters -- the framework infers it from the Value.
1) Don't use Convert -- casting and Parsing is more efficient
You'll never get very far if all you do is follow instructions.
|
|
|
|
|
Hello Jassim,
It is because Indexing start from 0 not from 1.
Look,
Your gridContacts.RowCount will return 10, if you have 10 rows and your for loop will be executed 11 times. In grid, there are only 10 records, so last record will be blank line.
So,
start index from i = 1 or use i < gridContacts.RowCount
But by looking at your code, you should use i < gridContacts.RowCount. Otherwise you have to change some of your code too.
Thanks... Have Fun
|
|
|
|
|
There are two ways of using this. To make the correction on your code, it should be,
for (int i = 0; i < gridContacts.RowCount; i++)
{
}
Alternative way is to try with For each, which i prefer the best suit for Datagrid,
foreach (DataGridRow drv in gridContacts.Items)
{
}
|
|
|
|
|
Correction in For Loop-
for (int i = 0; i < gridContacts.RowCount; i++)
|
|
|
|
|
|
how to expire cookies best way in my Data Records project
|
|
|
|
|
A smidgen on research [^] will get you plenty of information. This search changing some of your words may be useful.
Ahh you want the BEST way, that will depend on your requirements, most just put in a short expiry date.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Please don't cross-post.
You'll never get very far if all you do is follow instructions.
|
|
|
|
|
I am using SharpGL to add a solid object to a scene by clicking a button.
private void InitElements(Scene scene)
{
var objectRoot = new SharpGL.SceneGraph.Primitives.Folder() { Name = "Root" };
scene.SceneContainer.AddChild(objectRoot);
var camera = GetCamera();
float cmX = camera.Position.X; float cmY = camera.Position.Y; float cmZ = camera.Position.Z;
float tgX = camera.Target.X; float tgY = camera.Target.Y; float tgZ = camera.Target.Z;
float upX = camera.UpVector.X; float upY = camera.UpVector.Y; float upZ = camera.UpVector.Z;
objectArcBallEffect = new ArcBallEffect(cmX, cmY, cmZ, tgX, tgY, tgZ, upX, upY, upZ);
objectRoot.AddEffect(objectArcBallEffect);
var axisRoot = new SharpGL.SceneGraph.Primitives.Folder() { Name = "axis root" };
scene.SceneContainer.AddChild(axisRoot);
axisArcBallEffect = new ArcBallEffect(cmX, cmY, cmZ, tgX, tgY, tgZ, upX, upY, upZ);
axisRoot.AddEffect(axisArcBallEffect);
InitLight(objectRoot);
InitAxis(objectRoot);
InitAxis(axisRoot);
InitFrameElement(6, 24, 7, objectRoot);
}
After I initiate the solid elements in scene, I would like to add a solid object to that scene by clicking a button. I successfully add it in scene. But I cannot drag and rotate it with the initial elements in the scene. It means I didn't successfully add it to the same scene. Thanks in advance.
private void btnAddEllement_Click(object sender, EventArgs e)
{
var objectRoot = new SharpGL.SceneGraph.Primitives.Folder() { Name = "Root" };
var scene = this.sceneControl1.Scene;
addCylinder(objectRoot, 50, 30, 100, 5);
scene.SceneContainer.AddChild(objectRoot);
}
|
|
|
|
|
There's far too many silly questions appearing on this forum, like 'Please write my project for me'. This place used to be the best forum around, and the first place I would turn to, but trying to wade through all these inane questions is just too much.
Has anyone got any ideas as to where I can go with a more erudite audience?
|
|
|
|
|
Mars.
The quality of questions has plummeted on every site out there. We don't have a monopoly on idiots here.
|
|
|
|