[python-win32] Dispatch vs EnsureDispatch performance

Mark Hammond skippy.hammond at gmail.com
Wed Apr 17 11:19:28 EDT 2024


I'm mildly surprised by that - a profiler might show some low-hanging 
fruit, and/or might show different characteristics when many more 
functions are used. However, the primary reason for EnsureDispatch is 
for better support of the object model - there's far more context 
available and this less chance of upsetting some COM objects - eg, when 
`foo.bar` is seen, EnsureDispatch knows for sure that `bar` is a method, 
but dynamic dispatch doesn't know if the resulting object is going to be 
called or not.

HTH,

Mark

On 2024-04-17 2:07 a.m., Sven Bardos via python-win32 wrote:
> Hi,
>
> shouldn't be EnsureDispatch be faster than Dispatch once the code 
> generation is done?
>
> I've measured it by calling 6000 COM calls like this:
>
> dirpath = Path('C:/Users/sbardos/AppData/Local/Temp/gen_py/3.10/')
> if dirpath.exists() and dirpath.is_dir():
> shutil.rmtree(dirpath)
>
> app = Dispatch("CT.Application")
> job = app.CreateJobObject()
>
> start = timer()
>
> for i in range(2000):
> cnt, devIds = job.GetAllDeviceIds()
> cnt, sheetIds = job.GetSheetIds()
> dev = job.CreateDeviceObject()
>
> end = timer()
> print(f"Time ellapsed (late): {end - start}s")
> and the ensure Dispatch version:
> app = EnsureDispatch("CT.Application")
> job = app.CreateJobObject()
> start = timer()
> for i in range(2000):
> cnt, devIds = job.GetAllDeviceIds(None)
> cnt, sheetIds = job.GetSheetIds(None)
> dev = job.CreateDeviceObject()
> end = timer()
> print(f"Time ellapsed (early): {end - start}s")
> EnsureDispatch is a little bit slower ~4.2s compared to ~4.0s.
> If I don't get a performance boost with EnsureDispatch, is there even 
> a point using it?
>
> Thanks,
> Sven
>
> _______________________________________________
> python-win32 mailing list
> python-win32 at python.org
> https://mail.python.org/mailman/listinfo/python-win32
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.python.org/pipermail/python-win32/attachments/20240417/15cb7d23/attachment.html>


More information about the python-win32 mailing list