明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
12
返回列表 发新帖
楼主: chpmould

[图元] 集合对象转换为Entity对象

  [复制链接]
发表于 2010-12-12 06:39 | 显示全部楼层
另外Id.GetObject的耗时远远大于tr.GetObject的

还有这事?真没测试过。
 楼主| 发表于 2010-12-12 08:24 | 显示全部楼层
谢谢老师的指导...
发表于 2010-12-12 13:06 | 显示全部楼层
测试代码
  1.         [CommandMethod("tt1")]
  2.         public static void test21()
  3.         {
  4.             Document doc = Application.DocumentManager.MdiActiveDocument;
  5.             Database db = doc.Database;
  6.             Editor ed = doc.Editor;

  7.             Random rand = new Random();
  8.             int num = ed.GetInteger("\n输入点数:").Value;

  9.             using (var tr = db.TransactionManager.StartTransaction())
  10.             {

  11.                 db.Pdmode = 35;
  12.                 db.Pdsize = -2;

  13.                 var pnts =
  14.                     Enumerable
  15.                     .Range(0, num)
  16.                     .Select(i => new DBPoint(new Point3d(rand.NextDouble() * 100, rand.NextDouble() * 100, 0)));

  17.                 var btr = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
  18.                 foreach (var pnt in pnts)
  19.                 {
  20.                     btr.AppendEntity(pnt);
  21.                     tr.AddNewlyCreatedDBObject(pnt, true);
  22.                 }
  23.                 tr.Commit();
  24.             }

  25.         }
  1.         [CommandMethod("transtest")]
  2.         public static void TransTest()
  3.         {

  4.             var db = HostApplicationServices.WorkingDatabase;
  5.             var doc = Application.DocumentManager.GetDocument(db);
  6.             var ed = doc.Editor;

  7.             var resSel = ed.SelectAll();
  8.             if (resSel.Status != PromptStatus.OK) return;

  9.             var ss = resSel.Value.GetObjectIds();
  10.             ed.WriteMessage("\n总共{0}个实体", ss.Length);

  11.             Stopwatch watch = new Stopwatch();

  12.             watch.Start();
  13.             using (Transaction tr = db.TransactionManager.StartTransaction())
  14.             {
  15.                 foreach (var id in ss)
  16.                 {
  17.                     Entity ent = tr.GetObject(id, OpenMode.ForWrite) as Entity;
  18.                     int color = ent.ColorIndex;
  19.                     ent.ColorIndex = 1;
  20.                 }
  21.                 tr.Commit();
  22.             }
  23.             watch.Stop();
  24.             ed.WriteMessage("\n单事务使用tr.GetObject(StartTransaction)耗时:{0}毫秒", watch.ElapsedMilliseconds);

  25.             //watch.Reset();
  26.             //watch.Start();
  27.             //using (Transaction tr = db.TransactionManager.StartOpenCloseTransaction())
  28.             //{
  29.             //    foreach (var id in ss)
  30.             //    {
  31.             //        Entity ent = tr.GetObject(id, OpenMode.ForWrite) as Entity;
  32.             //        int color = ent.ColorIndex;
  33.             //        ent.ColorIndex = 2;
  34.             //    }
  35.             //    tr.Commit();
  36.             //}
  37.             //watch.Stop();
  38.             //ed.WriteMessage("\n单事务使用tr.GetObject(StartOpenCloseTransaction)耗时:{0}毫秒", watch.ElapsedMilliseconds);

  39.             watch.Reset();
  40.             watch.Start();
  41.             using (Transaction tr = db.TransactionManager.StartTransaction())
  42.             {
  43.                 foreach (var id in ss)
  44.                 {
  45.                     Entity ent = id.GetObject(OpenMode.ForWrite) as Entity;
  46.                     int color = ent.ColorIndex;
  47.                     ent.ColorIndex = 3;
  48.                 }
  49.                 tr.Commit();
  50.             }
  51.             watch.Stop();
  52.             ed.WriteMessage("\n单事务使用id.GetObject耗时:{0}毫秒", watch.ElapsedMilliseconds);

  53.             watch.Reset();
  54.             watch.Start();
  55.             foreach (var id in ss)
  56.             {
  57.                 using (Transaction tr = db.TransactionManager.StartTransaction())
  58.                 {
  59.                     Entity ent = tr.GetObject(id, OpenMode.ForWrite) as Entity;
  60.                     int color = ent.ColorIndex;
  61.                     ent.ColorIndex = 4;
  62.                     tr.Commit();
  63.                 }
  64.             }
  65.             watch.Stop();
  66.             ed.WriteMessage("\n多事务使用tr.GetObject(StartTransaction)耗时:{0}毫秒", watch.ElapsedMilliseconds);

  67.             //watch.Reset();
  68.             //watch.Start();
  69.             //foreach (var id in ss)
  70.             //{
  71.             //    using (Transaction tr = db.TransactionManager.StartOpenCloseTransaction())
  72.             //    {
  73.             //        Entity ent = tr.GetObject(id, OpenMode.ForWrite) as Entity;
  74.             //        int color = ent.ColorIndex;
  75.             //        ent.ColorIndex = 5;
  76.             //        tr.Commit();
  77.             //    }
  78.             //}
  79.             //watch.Stop();
  80.             //ed.WriteMessage("\n多事务使用tr.GetObject(StartOpenCloseTransaction)耗时:{0}毫秒", watch.ElapsedMilliseconds);

  81.             watch.Reset();
  82.             watch.Start();
  83.             foreach (var id in ss)
  84.             {
  85.                 using (Transaction tr = db.TransactionManager.StartTransaction())
  86.                 {

  87.                     Entity ent = id.GetObject(OpenMode.ForWrite) as Entity;
  88.                     int color = ent.ColorIndex;
  89.                     ent.ColorIndex = 6;
  90.                     tr.Commit();
  91.                 }
  92.             }
  93.             watch.Stop();
  94.             ed.WriteMessage("\n多事务使用id.GetObject耗时:{0}毫秒", watch.ElapsedMilliseconds);


  95.         }
结果:
  1. 命令: transtest
  2. 总共10000个实体
  3. 单事务使用tr.GetObject(StartTransaction)耗时:394毫秒
  4. 单事务使用id.GetObject耗时:322毫秒
  5. 多事务使用tr.GetObject(StartTransaction)耗时:642毫秒
  6. 多事务使用id.GetObject耗时:676毫秒
  7. 命令:  TRANSTEST
  8. 总共10000个实体
  9. 单事务使用tr.GetObject(StartTransaction)耗时:329毫秒
  10. 单事务使用id.GetObject耗时:318毫秒
  11. 多事务使用tr.GetObject(StartTransaction)耗时:631毫秒
  12. 多事务使用id.GetObject耗时:667毫秒
  13. 命令: tt1
  14. input number of points: 50000
  15. 命令: transtest
  16. 总共60000个实体
  17. 单事务使用tr.GetObject(StartTransaction)耗时:1773毫秒
  18. 单事务使用id.GetObject耗时:1963毫秒
  19. 多事务使用tr.GetObject(StartTransaction)耗时:3899毫秒
  20. 多事务使用id.GetObject耗时:4155毫秒
  21. 命令: tt1
  22. input number of points: 40000
  23. 命令: transtest
  24. 总共100000个实体
  25. 单事务使用tr.GetObject(StartTransaction)耗时:3011毫秒
  26. 单事务使用id.GetObject耗时:3426毫秒
  27. 多事务使用tr.GetObject(StartTransaction)耗时:6832毫秒
  28. 多事务使用id.GetObject耗时:7142毫秒
复制代码
发表于 2010-12-12 13:10 | 显示全部楼层
在单事务中两者相差并不大,甚至实体较少时id.GetObject还快些
多事务id.GetObject就很慢了
另外高版本使用StartOpenCloseTransaction效率更好
这个可以看看忽悠悠的测试
http://www.objectarx.net/forum.p ... &extra=page%3D1

发表于 2010-12-12 14:55 | 显示全部楼层
ID.getobject在没有事务打开的时候就会出错。
发表于 2010-12-12 16:38 | 显示全部楼层
01.命令: transtest
02.总共10000个实体
03.单事务使用tr.GetObject(StartTransaction)耗时:394毫秒

这些测试很有意义,效果很明显,看来处理多个对象时应该用tr.Getobject,反正也不增加代码量。
估计,Transaction与OpenCloseTransaction应该各有其适用性,只是目前文档匮乏,找不到更多依据。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-5-13 06:22 , Processed in 0.120732 second(s), 16 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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