hi @_gile
thanks for the tip about Try Cast i always find smth helpful in your posts,
i tried both methods and here is the result:
Command:
Command: neltao
Unknown command "NELTAO". Press F1 for help.
Command: NETLOAD
Command: FORVSFOREACH
entities count: 62,389.00
try cast:
foreach1: 436.00
forloop: 356.00
without try cast:
foreach2: 370.00
forloop2: 362.00
Command: FORVSFOREACH
entities count: 62,389.00
try cast:
foreach1: 422.00
forloop: 398.00
without try cast:
foreach2: 362.00
forloop2: 365.00
Command: FORVSFOREACH
entities count: 62,389.00
try cast:
foreach1: 413.00
forloop: 370.00
without try cast:
foreach2: 377.00
forloop2: 351.00
Command: FORVSFOREACH
entities count: 62,389.00
try cast:
foreach1: 377.00
forloop: 358.00
without try cast:
foreach2: 363.00
forloop2: 402.00
Command: FORVSFOREACH
entities count: 62,389.00
try cast:
foreach1: 365.00
forloop: 350.00
without try cast:
foreach2: 370.00
forloop2: 373.00
[CommandMethod("ForVsForeach")]
public void ForAndForeach()
{
#region curr doc
var adoc = Application.DocumentManager.MdiActiveDocument;
var adb = adoc.Database;
var editor = adoc.Editor;
var cdoc = cApp.CivilApplication.ActiveDocument;
#endregion
Stopwatch stopWatch = new Stopwatch();
var ids = editor.SelectAll().Value.GetObjectIds();
int count = ids.Length;
Entity entity;
var transform = Matrix3d.Displacement(new Vector3d(22.22, 33.33, 0));
editor.WriteMessage($"\n entities count: {count,0:#,0.00} \n");
//////////////////////////////// try cast /////////////////////////////////////////////
editor.WriteMessage($"\n\t try cast: \n");
stopWatch.Reset();
using (var tr = adb.TransactionManager.StartTransaction())
{
stopWatch.Start();
foreach (var id in ids)
{
entity = id.GetObject(OpenMode.ForWrite) as Entity;
entity.TransformBy(transform);
}
stopWatch.Stop();
//tr.Commit();
}
editor.WriteMessage($"\n\t\t\t foreach1: {stopWatch.ElapsedMilliseconds,0:#,0.00}\n");
stopWatch.Reset();
using (var tr = adb.TransactionManager.StartTransaction())
{
stopWatch.Start();
for (int i = 0; i < count; i++)
{
entity = ids[i].GetObject(OpenMode.ForWrite) as Entity;
entity.TransformBy(transform);
}
stopWatch.Stop();
//tr.Commit();
}
editor.WriteMessage($"\n\n\t\t\t forloop: {stopWatch.ElapsedMilliseconds,0:#,0.00}\n");
////////////////////////////////without try cast/////////////////////////////////////////////
editor.WriteMessage($"\n\t without try cast: \n");
stopWatch.Reset();
using (var tr = adb.TransactionManager.StartTransaction())
{
stopWatch.Start();
foreach (var id in ids)
{
entity = (Entity)id.GetObject(OpenMode.ForWrite);
entity.TransformBy(transform);
}
stopWatch.Stop();
//tr.Commit();
}
editor.WriteMessage($"\n\n\t\t\t foreach2: {stopWatch.ElapsedMilliseconds,0:#,0.00}\n");
stopWatch.Reset();
using (var tr = adb.TransactionManager.StartTransaction())
{
stopWatch.Start();
for (int i = 0; i < count; i++)
{
entity = (Entity)ids[i].GetObject(OpenMode.ForWrite);
entity.TransformBy(transform);
}
stopWatch.Stop();
//tr.Commit();
}
editor.WriteMessage($"\n\n\t\t\t forloop2: {stopWatch.ElapsedMilliseconds,0:#,0.00}\n\n");
}