[python-win32] speed up win32com.client

Tom tom at pentest.7safe.com
Wed May 9 16:33:57 CEST 2012


Sorry to interject here but I have a question along the same vein:
If I have a script (in this case that interacts with Word through
win32com.client) with statements like this:
doc.ActiveWindow.Selection.BoldRun()
doc.ActiveWindow.Selection.TypeText(_type)
doc.ActiveWindow.Selection.BoldRun()

Does each line require 3 COM calls, one for ActiveWindow, one for Selection
and one for the method being called, or is the Selection object cached
somewhere?
The question really is the code below more efficient than the code above in
terms of COM calls, or is the difference minimal?
selec = doc.ActiveWindow.Selection
selec.BoldRun()
selec.TypeText(_type)
selec.BoldRun()

Its something I have always wondered. Thanks!

On Tue, May 8, 2012 at 5:45 PM, Tim Roberts <timr at probo.com> wrote:

> DANIEL POSE wrote:
> > Hello,
> >
> > I am writing code to program AutoCAD access from Python using pywin32.
> > When I need to work with a high number of AutoCAD objects, it is
> > faster to use vba than python.
> > Are there some way to speed up python code in order to work faster
> > with AutoCAD elements?
> > For example in the next code when I work with 512 AutoCAD blocks:
> > ...
> >
> > The output for the code is the following:
> >
> > M_dimension= 512
> > R_dimension= 262144
> > t_block1= 4.25343304805
> > t_block2= 3.88635510938
> > t_block3= 0.487477319045
> >
> >
> > Then it is faster to work with R than M, even though R is bigger.
> >
>
> I hope that's not a surprise to you.  The first loop (block2) involves
> two calls into the AutoCAD COM object in each iteration.  The second
> loop (block3) is simply manipulating lists of integers, entirely within
> Python.
>
> > Some suggestions for speed up pywin32 code in this example?
> >
>
> There isn't really anything here to speed up.  You're just "glue".  The
> work is being done in AutoCAD.  It is almost impossible for me to
> believe that Visual Basic does this same loop any faster.
>
> However, you can certainly try switching to early binding by using:
>    acad= win32com.client.gencache.EnsureDispatch("AutoCAD.Application")
>
> --
> Tim Roberts, timr at probo.com
> Providenza & Boekelheide, Inc.
>
> _______________________________________________
> python-win32 mailing list
> python-win32 at python.org
> http://mail.python.org/mailman/listinfo/python-win32
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-win32/attachments/20120509/947386ae/attachment.html>


More information about the python-win32 mailing list