|
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.
|
|
|
|
|
What time's the next bus?
|
|
|
|
|
20:24[^] - but book your ticket early to avoid disappointment!
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
Try dipping your nose into Q&A for a taste of low quality questions. You might want to take your comment to the bugs @ sugs forum.
Also such a comment coming from someone who has almost no involvement with the site, 28 post in 3 years, is a little ironic.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Some people have work to do, and can't afford much time to hang about reading silly questions. What time I do have I prefer to use wisely.
|
|
|
|
|
With an attitude like that, you won't be missed.
|
|
|
|
|
Just give up. There's no place that consistently has questions worth anyone's time. The only way to find such questions is, unfortunately, to read through piles of excrement.
|
|
|
|