找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1340|回复: 2

[分享] ObjectId.GetObject vs. Transaction.GetObject in AutoCAD .NET

[复制链接]

已领礼包: 859个

财富等级: 财运亨通

发表于 2014-5-4 16:57:25 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
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.
  1. [CommandMethod("IdGetObjVsTransGetObj2")]
  2. public static void IdGetObjVsTransGetObj2_Method()
  3. {
  4.     Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
  5.     Database db = HostApplicationServices.WorkingDatabase;
  6.     try
  7.     {
  8.         ObjectId id = Autodesk.AutoCAD.Internal.Utils.EntLast();
  9.         if (!id.IsNull && id.IsValid)
  10.         {
  11.             Autodesk.AutoCAD.DatabaseServices.TransactionManager dbTM = db.TransactionManager;
  12.             int count = 15000;
  13.             int color;
  14.             Stopwatch watch = new Stopwatch();

  15.             watch.Restart();
  16.             using (Transaction tr = dbTM.StartTransaction())
  17.             {
  18.                 for (int i = 0; i < count; i++)
  19.                 {
  20.                     Entity ent = (Entity)id.GetObject(OpenMode.ForRead);
  21.                     color = ent.ColorIndex;
  22.                 }

  23.                 tr.Commit();
  24.             }
  25.             watch.Stop();
  26.             ed.WriteMessage("Time elapsed for ObjectId.GetObject() on an entity {0} times: {1}\n", count, watch.Elapsed.TotalMilliseconds);

  27.             watch.Restart();
  28.             using (Transaction tr = dbTM.StartTransaction())
  29.             {
  30.                 for (int i = 0; i < count; i++)
  31.                 {
  32.                     Entity ent = (Entity)tr.GetObject(id, OpenMode.ForRead);
  33.                     color = ent.ColorIndex;
  34.                 }

  35.                 tr.Commit();
  36.             }
  37.             watch.Stop();
  38.             ed.WriteMessage("Time elapsed for Transaction.GetObject() on an entity {0} times: {1}\n", count, watch.Elapsed.TotalMilliseconds);
  39.         }
  40.         else
  41.         {
  42.             ed.WriteMessage("\nThere is no entity at all in the drawing.");
  43.         }
  44.     }
  45.     catch (System.Exception ex)
  46.     {
  47.         MgdAcApplication.DocumentManager.MdiActiveDocument.Editor.WriteMessage(ex.ToString());
  48.     }
  49. }

  50. [CommandMethod("IdGetObjVsTransGetObj")]
  51. public static void IdGetObjVsTransGetObj_Method()
  52. {
  53.     Editor ed = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
  54.     Database db = HostApplicationServices.WorkingDatabase;
  55.     try
  56.     {
  57.         Autodesk.AutoCAD.DatabaseServices.TransactionManager dbTM = db.TransactionManager;
  58.         int index = 0;
  59.         int color;
  60.         Stopwatch watch = new Stopwatch();

  61.         watch.Restart();
  62.         using (Transaction tr = dbTM.StartTransaction())
  63.         {
  64.             BlockTableRecord btr = tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForRead) as BlockTableRecord;
  65.             foreach (ObjectId id in btr)
  66.             {
  67.                 Entity ent = (Entity)id.GetObject(OpenMode.ForRead);
  68.                 color = ent.ColorIndex;
  69.                 index++;
  70.             }

  71.             tr.Commit();
  72.         }
  73.         watch.Stop();
  74.         ed.WriteMessage("Time elapsed using ObjectId.GetObject() against {0} entities: {1}\n", index, watch.Elapsed.TotalMilliseconds);
  75.                
  76.         index = 0;
  77.         watch.Restart();
  78.         using (Transaction tr = dbTM.StartTransaction())
  79.         {
  80.             BlockTableRecord btr = tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForRead) as BlockTableRecord;
  81.             foreach (ObjectId id in btr)
  82.             {
  83.                 Entity ent = (Entity)tr.GetObject(id, OpenMode.ForRead);
  84.                 color = ent.ColorIndex;
  85.                 index++;
  86.             }

  87.             tr.Commit();
  88.         }
  89.         watch.Stop();
  90.         ed.WriteMessage("Time elapsed using Transaction.GetObject() against {0} entities: {1}\n", index, watch.Elapsed.TotalMilliseconds);               
  91.     }
  92.     catch (System.Exception ex)
  93.     {
  94.         MgdAcApplication.DocumentManager.MdiActiveDocument.Editor.WriteMessage(ex.ToString());
  95.     }
  96. }

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!


论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!

已领礼包: 859个

财富等级: 财运亨通

 楼主| 发表于 2014-5-4 20:34:21 来自手机 | 显示全部楼层
Mark
1  计时 Stopwatch watch = new …
2   ObjectId.GetObject(OpenMode.ForRead) 更高效
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

已领礼包: 859个

财富等级: 财运亨通

 楼主| 发表于 2014-5-5 06:56:56 来自手机 | 显示全部楼层
3 ObjectId id = Autodesk.AutoCAD.Internal.Utils.EntLast();
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-11-17 22:16 , Processed in 0.173209 second(s), 31 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表