本帖最后由 作者 于 2009-6-7 13:38:35 编辑
用这两种开发方法,画100000个圆,看看要多少时间。使用环境:AutoCAD 2007
VBA 代码:- Sub Test()
- Dim s As Double
- s = ThisDrawing.GetVariable("date")
- s = (s - Fix(s)) * 86400#
-
- Dim cenPt(2) As Double
- For i = 0 To 99999
- ThisDrawing.ModelSpace.AddCircle cenPt, 100#
- Next
-
- Dim e As Double
- e = ThisDrawing.GetVariable("date")
- e = (e - Fix(e)) * 86400#
-
- MsgBox ("VBA画100000个圆所用时间:" & e - s & "秒")
- End Sub
C# 代码:- // using System.Diagnostics;
- using Autodesk.AutoCAD.ApplicationServices;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.EditorInput;
- using Autodesk.AutoCAD.Geometry;
- using Autodesk.AutoCAD.Runtime;
- namespace Test
- {
- public class Class1
- {
- [CommandMethod("Test")]
- public void MyTest()
- {
- double s = (double)Application.GetSystemVariable("date");
- s = (s - System.Math.Floor(s)) * 86400.0;
- /*
- Stopwatch sw = new Stopwatch();
- sw.Start();
- */
- Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
- Database db = HostApplicationServices.WorkingDatabase;
- Circle ent = null;
- using (Transaction trans = db.TransactionManager.StartTransaction())
- {
- BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId,
- OpenMode.ForRead);
- BlockTableRecord btr = (BlockTableRecord)trans.GetObject
- (bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
- for (int i = 0; i < 100000; i++)
- {
- ent = new Circle(Point3d.Origin, Vector3d.ZAxis, 100.0);
- btr.AppendEntity(ent);
- trans.AddNewlyCreatedDBObject(ent, true);
- }
- trans.Commit();
- }
- /*
- sw.Stop();
- ed.WriteMessage("\nC#画100000个圆所用时间: {0}分钟 {1}秒 {2}毫秒",
- sw.Elapsed.Minutes, sw.Elapsed.Seconds, sw.Elapsed.Milliseconds);
- */
- double e = (double)Application.GetSystemVariable("date");
- e = (e - System.Math.Floor(e)) * 86400.0;
- Application.ShowAlertDialog("\nC#画100000个圆所用时间: " + (e - s) + "秒");
- }
- }
- }
AutoLisp 代码:- (defun c:test (/ CPT ETIME I STIME UTIME)
- (setq cpt '(0.0 0.0 0.0)
- STIME (getvar "date")
- )
- (repeat 100000
- (entmake (APPEND '((0 . "CIRCLE")
- (100 . "AcDbEntity")
- (100 . "AcDbCircle")
- (8 . "0")
- (62 . 1)
- )
- (LIST (CONS 10 cpt) (cons 40 100.0))
- )
- )
- )
- (setq ETIME (getvar "date")
- UTIME (rtos (* 86400.0 (- (- ETIME STIME) (fix (- ETIME STIME))))
- 2
- 8
- )
- )
- (princ (strcat "\n我们用LISP画100000个圆要用: " UTIME "秒"))
- )
VisualLisp 代码:- (defun c:test (/ CPT ETIME I PACE STIME UTIME VCPT)
- (setq cpt '(0.0 0.0 0.0)
- STIME (getvar "date")
- pace (vla-get-modelspace
- (vla-get-activedocument (vlax-get-acad-object))
- )
- vcpt (vlax-3d-point cpt)
- )
- (rtos STIME 2 2)
- (repeat 100000
- (vla-AddCircle pace vcpt 100.0)
- )
- (setq ETIME (getvar "date")
- UTIME (rtos (* 86400.0 (- (- ETIME STIME) (fix (- ETIME STIME))))
- 2
- 8
- )
- )
- (princ (strcat "\n我们用VLISP画100000个圆要用: " UTIME "秒"))
- (princ)
- )
为了公平起见,都用系统变量进行计时。
看完这个帖子,你是否觉得应该从VBA转到C#?
|