[python-win32] speed up win32com.client

Mark Hammond skippy.hammond at gmail.com
Sat May 12 05:32:53 CEST 2012


On 10/05/2012 12:33 AM, Tom wrote:
> 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?

It requires 3 COM calls - there is no caching (and your code above is a 
good example of why - the "ActiveWindow" could easily change between the 
value being cached and it being reused).

> 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()

The above would be more efficient.

Cheers,

Mark
>
> Its something I have always wondered. Thanks!
>
> On Tue, May 8, 2012 at 5:45 PM, Tim Roberts <timr at probo.com
> <mailto: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 <mailto:timr at probo.com>
>     Providenza & Boekelheide, Inc.
>
>     _______________________________________________
>     python-win32 mailing list
>     python-win32 at python.org <mailto:python-win32 at python.org>
>     http://mail.python.org/mailman/listinfo/python-win32
>
>
>
>
> _______________________________________________
> python-win32 mailing list
> python-win32 at python.org
> http://mail.python.org/mailman/listinfo/python-win32
>




More information about the python-win32 mailing list