- UID
- 658062
- 积分
- 2147
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2008-10-22
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
ObjectId.GetObject vs. Transaction.GetObject in AutoCAD .NET
We introduced the two ways (ObjectId.GetObject and Transaction.GetObject) to open AutoCAD .NET objects, demonstrated with code examples, and analysized some data individually before.
In this article, let’s see what differences between them especially in the performance concern.
We are going to do two small experiments here, one is to open the same last entity in the database with the two approaches 15000 times each, and the other is to open 15000 different entities once each in the database with the two approaches. Here are the code and test commands.
- [CommandMethod("IdGetObjVsTransGetObj2")]
- public static void IdGetObjVsTransGetObj2_Method()
- {
- Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
- Database db = HostApplicationServices.WorkingDatabase;
- try
- {
- ObjectId id = Autodesk.AutoCAD.Internal.Utils.EntLast();
- if (!id.IsNull && id.IsValid)
- {
- Autodesk.AutoCAD.DatabaseServices.TransactionManager dbTM = db.TransactionManager;
- int count = 15000;
- int color;
- Stopwatch watch = new Stopwatch();
- watch.Restart();
- using (Transaction tr = dbTM.StartTransaction())
- {
- for (int i = 0; i < count; i++)
- {
- Entity ent = (Entity)id.GetObject(OpenMode.ForRead);
- color = ent.ColorIndex;
- }
- tr.Commit();
- }
- watch.Stop();
- ed.WriteMessage("Time elapsed for ObjectId.GetObject() on an entity {0} times: {1}\n", count, watch.Elapsed.TotalMilliseconds);
- watch.Restart();
- using (Transaction tr = dbTM.StartTransaction())
- {
- for (int i = 0; i < count; i++)
- {
- Entity ent = (Entity)tr.GetObject(id, OpenMode.ForRead);
- color = ent.ColorIndex;
- }
- tr.Commit();
- }
- watch.Stop();
- ed.WriteMessage("Time elapsed for Transaction.GetObject() on an entity {0} times: {1}\n", count, watch.Elapsed.TotalMilliseconds);
- }
- else
- {
- ed.WriteMessage("\nThere is no entity at all in the drawing.");
- }
- }
- catch (System.Exception ex)
- {
- MgdAcApplication.DocumentManager.MdiActiveDocument.Editor.WriteMessage(ex.ToString());
- }
- }
- [CommandMethod("IdGetObjVsTransGetObj")]
- public static void IdGetObjVsTransGetObj_Method()
- {
- Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
- Database db = HostApplicationServices.WorkingDatabase;
- try
- {
- Autodesk.AutoCAD.DatabaseServices.TransactionManager dbTM = db.TransactionManager;
- int index = 0;
- int color;
- Stopwatch watch = new Stopwatch();
- watch.Restart();
- using (Transaction tr = dbTM.StartTransaction())
- {
- BlockTableRecord btr = tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForRead) as BlockTableRecord;
- foreach (ObjectId id in btr)
- {
- Entity ent = (Entity)id.GetObject(OpenMode.ForRead);
- color = ent.ColorIndex;
- index++;
- }
- tr.Commit();
- }
- watch.Stop();
- ed.WriteMessage("Time elapsed using ObjectId.GetObject() against {0} entities: {1}\n", index, watch.Elapsed.TotalMilliseconds);
-
- index = 0;
- watch.Restart();
- using (Transaction tr = dbTM.StartTransaction())
- {
- BlockTableRecord btr = tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForRead) as BlockTableRecord;
- foreach (ObjectId id in btr)
- {
- Entity ent = (Entity)tr.GetObject(id, OpenMode.ForRead);
- color = ent.ColorIndex;
- index++;
- }
- tr.Commit();
- }
- watch.Stop();
- ed.WriteMessage("Time elapsed using Transaction.GetObject() against {0} entities: {1}\n", index, watch.Elapsed.TotalMilliseconds);
- }
- catch (System.Exception ex)
- {
- MgdAcApplication.DocumentManager.MdiActiveDocument.Editor.WriteMessage(ex.ToString());
- }
- }
Here is the output:
Command: IdGetObjVsTransGetObj2
Time elapsed for ObjectId.GetObject() on an entity 15000 times: 125.888
Time elapsed for Transaction.GetObject() on an entity 15000 times: 53.8417
Command: IdGetObjVsTransGetObj
Time elapsed using ObjectId.GetObject() against 15000 entities: 1441.1063
Time elapsed using Transaction.GetObject() against 15000 entities: 4565.8946
The results are pretty out of expectations, aren’t they?
In addition, they are not consistent with each other! The ObjectId.GetObject() takes more time to open the same object many times than the Transaction.GetObject(), about 1.5 times more; but takes mush less to open many different objects, less than one-third.
How come? What are going on there?
Hmmm …. no idea. But the experiments and data here kind of guide us which way to choose in what situations if application performance is a big concern. By the way, only reading operations are performed in the experiments. If interested, please feel free to try the writing operations as well. Though the abosolute data must be different, I believe the trend is the same. Have fun!
|
|