明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 4178|回复: 4

[运行时] C#与VBA的速度比较

[复制链接]
发表于 2009-6-5 21:36 | 显示全部楼层 |阅读模式
本帖最后由 作者 于 2009-6-7 13:38:35 编辑

用这两种开发方法,画100000个圆,看看要多少时间。使用环境:AutoCAD 2007
VBA 代码:
  1. Sub Test()
  2.     Dim s As Double
  3.     s = ThisDrawing.GetVariable("date")
  4.     s = (s - Fix(s)) * 86400#
  5.    
  6.     Dim cenPt(2) As Double
  7.     For i = 0 To 99999
  8.         ThisDrawing.ModelSpace.AddCircle cenPt, 100#
  9.     Next
  10.    
  11.     Dim e As Double
  12.     e = ThisDrawing.GetVariable("date")
  13.     e = (e - Fix(e)) * 86400#
  14.    
  15.     MsgBox ("VBA画100000个圆所用时间:" & e - s & "秒")
  16. End Sub
C# 代码:
  1. // using System.Diagnostics;
  2. using Autodesk.AutoCAD.ApplicationServices;
  3. using Autodesk.AutoCAD.DatabaseServices;
  4. using Autodesk.AutoCAD.EditorInput;
  5. using Autodesk.AutoCAD.Geometry;
  6. using Autodesk.AutoCAD.Runtime;
  7. namespace Test
  8. {
  9.     public class Class1
  10.     {
  11.         [CommandMethod("Test")]
  12.         public void MyTest()
  13.         {
  14.             double s = (double)Application.GetSystemVariable("date");
  15.             s = (s - System.Math.Floor(s)) * 86400.0;
  16.             /*
  17.             Stopwatch sw = new Stopwatch();
  18.             sw.Start();
  19.             */
  20.             Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
  21.             Database db = HostApplicationServices.WorkingDatabase;
  22.             Circle ent = null;
  23.             using (Transaction trans = db.TransactionManager.StartTransaction())
  24.             {
  25.                 BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId,
  26.                     OpenMode.ForRead);
  27.                 BlockTableRecord btr = (BlockTableRecord)trans.GetObject
  28.                     (bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
  29.                 for (int i = 0; i < 100000; i++)
  30.                 {
  31.                     ent = new Circle(Point3d.Origin, Vector3d.ZAxis, 100.0);
  32.                     btr.AppendEntity(ent);
  33.                     trans.AddNewlyCreatedDBObject(ent, true);
  34.                 }
  35.                 trans.Commit();
  36.             }
  37.             /*
  38.             sw.Stop();
  39.             ed.WriteMessage("\nC#画100000个圆所用时间: {0}分钟 {1}秒 {2}毫秒",
  40.                 sw.Elapsed.Minutes, sw.Elapsed.Seconds, sw.Elapsed.Milliseconds);
  41.             */
  42.             double e = (double)Application.GetSystemVariable("date");
  43.             e = (e - System.Math.Floor(e)) * 86400.0;
  44.             Application.ShowAlertDialog("\nC#画100000个圆所用时间: " + (e - s) + "秒");
  45.         }
  46.     }
  47. }
AutoLisp 代码:
  1. (defun c:test (/ CPT ETIME I STIME UTIME)
  2.   (setq cpt   '(0.0 0.0 0.0)
  3. STIME (getvar "date")
  4.   )
  5.   (repeat 100000
  6.     (entmake (APPEND '((0 . "CIRCLE")
  7.          (100 . "AcDbEntity")
  8.          (100 . "AcDbCircle")
  9.          (8 . "0")
  10.          (62 . 1)
  11.         )
  12.        (LIST (CONS 10 cpt) (cons 40 100.0))
  13.       )
  14.     )
  15.   )
  16.   (setq ETIME (getvar "date")
  17. UTIME (rtos (* 86400.0 (- (- ETIME STIME) (fix (- ETIME STIME))))
  18.       2
  19.       8
  20.        )
  21.   )
  22.   (princ (strcat "\n我们用LISP画100000个圆要用: " UTIME "秒"))
  23. )
VisualLisp 代码:
  1. (defun c:test (/ CPT ETIME I PACE STIME UTIME VCPT)
  2.   (setq cpt   '(0.0 0.0 0.0)
  3. STIME (getvar "date")
  4. pace  (vla-get-modelspace
  5.   (vla-get-activedocument (vlax-get-acad-object))
  6.        )
  7. vcpt  (vlax-3d-point cpt)
  8.   )
  9.   (rtos STIME 2 2)
  10.   (repeat 100000
  11.     (vla-AddCircle pace vcpt 100.0)
  12.   )
  13.   (setq ETIME (getvar "date")
  14. UTIME (rtos (* 86400.0 (- (- ETIME STIME) (fix (- ETIME STIME))))
  15.       2
  16.       8
  17.        )
  18.   )
  19.   (princ (strcat "\n我们用VLISP画100000个圆要用: " UTIME "秒"))
  20.   (princ)
  21. )

为了公平起见,都用系统变量进行计时。
看完这个帖子,你是否觉得应该从VBA转到C#?

本帖子中包含更多资源

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

x
发表于 2009-6-5 21:59 | 显示全部楼层

其实VBA转到NetApi的理由应该很多,:)

效率上肯定比不上

其次,VBA在Cad里调用是永远的痛,要借助lisp实现

VBA的拖动要借助ObjectArx

VBA的曲线操作要借助Vlax,而且极不稳定

VBA的Bug太多

。。。。。。

发表于 2009-6-7 09:29 | 显示全部楼层
能否吧 lisp 和 arx的对比也加上?
发表于 2009-6-7 09:37 | 显示全部楼层

Lisp,老卢和我都是弱项:)

无痕兄可以写个测试代码试试

Lisp就我所知:

强在命令调用,但弱点也很多

使用命令过多,程序的控制很困难

Dcl很郁闷

单文档模式

效率很低

 楼主| 发表于 2009-6-7 13:41 | 显示全部楼层

修改了一下一楼的帖子。

要去监考了(高考),ARX代码,下次再加上。

注:LISP代码是BDYCAD写的。

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

本版积分规则

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

GMT+8, 2024-5-13 01:43 , Processed in 0.149387 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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