Dispatch vs EnsureDispatch performance

Sven Bardos via python-win32 <[email protected]> Wed, 17 Apr 2024 08:07:24 +0200
Newsgroups gmane.comp.python.windows
Message-ID <CAP10ovx+U1dzBBhBdm0fvjQ2+DJLmrX_fVSwrmTKuNE8gatQug@mail.gmail.com>
--===============3899270099336866958==
Content-Type: multipart/alternative; boundary="000000000000bedb4b061644ab95"

--000000000000bedb4b061644ab95
Content-Type: text/plain; charset="UTF-8"

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

--000000000000bedb4b061644ab95
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Hi,<div><br><div>shouldn&#39;t be EnsureDispatch be faster=
 than Dispatch once the code generation is done?</div><div><br></div><div>I=
&#39;ve measured it by calling 6000 COM calls like this:</div><div><br></di=
v><div><div style=3D"color:rgb(204,204,204);background-color:rgb(31,31,31);=
font-family:Consolas,&quot;Courier New&quot;,monospace;font-size:14px;line-=
height:19px;white-space:pre"><div><span style=3D"color:rgb(156,220,254)">di=
rpath</span> <span style=3D"color:rgb(212,212,212)">=3D</span> <span style=
=3D"color:rgb(78,201,176)">Path</span>(<span style=3D"color:rgb(206,145,120=
)">&#39;C:/Users/sbardos/AppData/Local/Temp/gen_py/3.10/&#39;</span>)</div>=
<div><span style=3D"color:rgb(197,134,192)">if</span> <span style=3D"color:=
rgb(156,220,254)">dirpath</span>.<span style=3D"color:rgb(220,220,170)">exi=
sts</span>() <span style=3D"color:rgb(86,156,214)">and</span> <span style=
=3D"color:rgb(156,220,254)">dirpath</span>.<span style=3D"color:rgb(220,220=
,170)">is_dir</span>():</div><div>=C2=A0 =C2=A0 <span style=3D"color:rgb(78=
,201,176)">shutil</span>.<span style=3D"color:rgb(156,220,254)">rmtree</spa=
n>(<span style=3D"color:rgb(156,220,254)">dirpath</span>)</div></div></div>=
<div><br></div><div><div style=3D"line-height:19px"><div style=3D"color:rgb=
(204,204,204);font-family:Consolas,&quot;Courier New&quot;,monospace;font-s=
ize:14px;white-space:pre;background-color:rgb(31,31,31)"><span style=3D"col=
or:rgb(156,220,254)">app</span> <span style=3D"color:rgb(212,212,212)">=3D<=
/span> <span style=3D"color:rgb(220,220,170)">Dispatch</span>(<span style=
=3D"color:rgb(206,145,120)">&quot;CT.Application&quot;</span>)</div><div st=
yle=3D"color:rgb(204,204,204);font-family:Consolas,&quot;Courier New&quot;,=
monospace;font-size:14px;white-space:pre;background-color:rgb(31,31,31)"><s=
pan style=3D"color:rgb(156,220,254)">job</span> <span style=3D"color:rgb(21=
2,212,212)">=3D</span> <span style=3D"color:rgb(156,220,254)">app</span>.Cr=
eateJobObject()</div><br><div style=3D"color:rgb(204,204,204);font-family:C=
onsolas,&quot;Courier New&quot;,monospace;font-size:14px;white-space:pre;ba=
ckground-color:rgb(31,31,31)"><span style=3D"color:rgb(156,220,254)">start<=
/span> <span style=3D"color:rgb(212,212,212)">=3D</span> <span style=3D"col=
or:rgb(156,220,254)">timer</span>()</div><br><div style=3D"color:rgb(204,20=
4,204);font-family:Consolas,&quot;Courier New&quot;,monospace;font-size:14p=
x;white-space:pre;background-color:rgb(31,31,31)"><span style=3D"color:rgb(=
197,134,192)">for</span> <span style=3D"color:rgb(156,220,254)">i</span> <s=
pan style=3D"color:rgb(197,134,192)">in</span> <span style=3D"color:rgb(78,=
201,176)">range</span>(<span style=3D"color:rgb(181,206,168)">2000</span>):=
</div><div style=3D"color:rgb(204,204,204);font-family:Consolas,&quot;Couri=
er New&quot;,monospace;font-size:14px;white-space:pre;background-color:rgb(=
31,31,31)">=C2=A0 =C2=A0 <span style=3D"color:rgb(156,220,254)">cnt</span>,=
 <span style=3D"color:rgb(156,220,254)">devIds</span> <span style=3D"color:=
rgb(212,212,212)">=3D</span> <span style=3D"color:rgb(156,220,254)">job</sp=
an>.GetAllDeviceIds()</div><div style=3D"color:rgb(204,204,204);font-family=
:Consolas,&quot;Courier New&quot;,monospace;font-size:14px;white-space:pre;=
background-color:rgb(31,31,31)">=C2=A0 =C2=A0 <span style=3D"color:rgb(156,=
220,254)">cnt</span>, <span style=3D"color:rgb(156,220,254)">sheetIds</span=
> <span style=3D"color:rgb(212,212,212)">=3D</span> <span style=3D"color:rg=
b(156,220,254)">job</span>.GetSheetIds()</div><div style=3D"color:rgb(204,2=
04,204);font-family:Consolas,&quot;Courier New&quot;,monospace;font-size:14=
px;white-space:pre;background-color:rgb(31,31,31)">=C2=A0 =C2=A0 <span styl=
e=3D"color:rgb(156,220,254)">dev</span> <span style=3D"color:rgb(212,212,21=
2)">=3D</span> <span style=3D"color:rgb(156,220,254)">job</span>.CreateDevi=
ceObject()</div><br><div style=3D"color:rgb(204,204,204);font-family:Consol=
as,&quot;Courier New&quot;,monospace;font-size:14px;white-space:pre;backgro=
und-color:rgb(31,31,31)"><span style=3D"color:rgb(156,220,254)">end</span> =
<span style=3D"color:rgb(212,212,212)">=3D</span> <span style=3D"color:rgb(=
156,220,254)">timer</span>()</div><div style=3D"color:rgb(204,204,204);font=
-family:Consolas,&quot;Courier New&quot;,monospace;font-size:14px;white-spa=
ce:pre;background-color:rgb(31,31,31)"><span style=3D"color:rgb(220,220,170=
)">print</span>(<span style=3D"color:rgb(86,156,214)">f</span><span style=
=3D"color:rgb(206,145,120)">&quot;Time ellapsed (late): </span><span style=
=3D"color:rgb(86,156,214)">{</span><span style=3D"color:rgb(156,220,254)">e=
nd</span> <span style=3D"color:rgb(212,212,212)">-</span> <span style=3D"co=
lor:rgb(156,220,254)">start</span><span style=3D"color:rgb(86,156,214)">}</=
span><span style=3D"color:rgb(206,145,120)">s&quot;</span>)</div><div style=
=3D"color:rgb(204,204,204);font-family:Consolas,&quot;Courier New&quot;,mon=
ospace;font-size:14px;white-space:pre;background-color:rgb(31,31,31)"><br><=
/div><div style=3D"color:rgb(204,204,204);font-family:Consolas,&quot;Courie=
r New&quot;,monospace;font-size:14px;white-space:pre;background-color:rgb(3=
1,31,31)">and the ensure Dispatch version:</div><div style=3D"color:rgb(204=
,204,204);font-family:Consolas,&quot;Courier New&quot;,monospace;font-size:=
14px;white-space:pre;background-color:rgb(31,31,31)"><br></div><div style=
=3D""><div style=3D"color:rgb(204,204,204);font-family:Consolas,&quot;Couri=
er New&quot;,monospace;font-size:14px;white-space:pre;background-color:rgb(=
31,31,31);line-height:19px"><div><span style=3D"color:rgb(156,220,254)">app=
</span> <span style=3D"color:rgb(212,212,212)">=3D</span> <span style=3D"co=
lor:rgb(220,220,170)">EnsureDispatch</span>(<span style=3D"color:rgb(206,14=
5,120)">&quot;CT.Application&quot;</span>)</div><div><span style=3D"color:r=
gb(156,220,254)">job</span> <span style=3D"color:rgb(212,212,212)">=3D</spa=
n> <span style=3D"color:rgb(156,220,254)">app</span>.CreateJobObject()</div=
><br><div><span style=3D"color:rgb(156,220,254)">start</span> <span style=
=3D"color:rgb(212,212,212)">=3D</span> <span style=3D"color:rgb(156,220,254=
)">timer</span>()</div><br><div><span style=3D"color:rgb(197,134,192)">for<=
/span> <span style=3D"color:rgb(156,220,254)">i</span> <span style=3D"color=
:rgb(197,134,192)">in</span> <span style=3D"color:rgb(78,201,176)">range</s=
pan>(<span style=3D"color:rgb(181,206,168)">2000</span>):</div><div>=C2=A0 =
=C2=A0 <span style=3D"color:rgb(156,220,254)">cnt</span>, <span style=3D"co=
lor:rgb(156,220,254)">devIds</span> <span style=3D"color:rgb(212,212,212)">=
=3D</span> <span style=3D"color:rgb(156,220,254)">job</span>.GetAllDeviceId=
s(<span style=3D"color:rgb(86,156,214)">None</span>)</div><div>=C2=A0 =C2=
=A0 <span style=3D"color:rgb(156,220,254)">cnt</span>, <span style=3D"color=
:rgb(156,220,254)">sheetIds</span> <span style=3D"color:rgb(212,212,212)">=
=3D</span> <span style=3D"color:rgb(156,220,254)">job</span>.GetSheetIds(<s=
pan style=3D"color:rgb(86,156,214)">None</span>)</div><div>=C2=A0 =C2=A0 <s=
pan style=3D"color:rgb(156,220,254)">dev</span> <span style=3D"color:rgb(21=
2,212,212)">=3D</span> <span style=3D"color:rgb(156,220,254)">job</span>.Cr=
eateDeviceObject()</div><br><div><span style=3D"color:rgb(156,220,254)">end=
</span> <span style=3D"color:rgb(212,212,212)">=3D</span> <span style=3D"co=
lor:rgb(156,220,254)">timer</span>()</div><div><span style=3D"color:rgb(220=
,220,170)">print</span>(<span style=3D"color:rgb(86,156,214)">f</span><span=
 style=3D"color:rgb(206,145,120)">&quot;Time ellapsed (early): </span><span=
 style=3D"color:rgb(86,156,214)">{</span><span style=3D"color:rgb(156,220,2=
54)">end</span> <span style=3D"color:rgb(212,212,212)">-</span> <span style=
=3D"color:rgb(156,220,254)">start</span><span style=3D"color:rgb(86,156,214=
)">}</span><span style=3D"color:rgb(206,145,120)">s&quot;</span>)</div><div=
><br></div><div><br></div></div>
EnsureDispatch is a little bit slower ~4.2s compared to ~4.0s.<br>If I don&=
#39;t get a performance boost with EnsureDispatch, is there even a point us=
ing it?<br><br>Thanks,<br>Sven<br></div></div></div></div></div>

--000000000000bedb4b061644ab95--

--===============3899270099336866958==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
python-win32 mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-win32

--===============3899270099336866958==--