Re: Dispatch vs EnsureDispatch performance

Mark Hammond <[email protected]> Wed, 17 Apr 2024 11:19:28 -0400
Newsgroups gmane.comp.python.windows
Message-ID <[email protected]>
This is a multi-part message in MIME format.
--===============7762019196568212731==
Content-Type: multipart/alternative;
 boundary="------------rVYnIzB20VvkyUvW8n8070BM"
Content-Language: en-US

This is a multi-part message in MIME format.
--------------rVYnIzB20VvkyUvW8n8070BM
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit

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
> [email protected]
> https://mail.python.org/mailman/listinfo/python-win32
--------------rVYnIzB20VvkyUvW8n8070BM
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 8bit

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <p>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.</p>
    <p>HTH,</p>
    <p>Mark<br>
    </p>
    <div class="moz-cite-prefix">On 2024-04-17 2:07 a.m., Sven Bardos
      via python-win32 wrote:<br>
    </div>
    <blockquote type="cite"
cite="mid:CAP10ovx+U1dzBBhBdm0fvjQ2+DJLmrX_fVSwrmTKuNE8gatQug@mail.gmail.com">
      <meta http-equiv="content-type" content="text/html; charset=UTF-8">
      <div dir="ltr">Hi,
        <div><br>
          <div>shouldn't be EnsureDispatch be faster than Dispatch once
            the code generation is done?</div>
          <div><br>
          </div>
          <div>I've measured it by calling 6000 COM calls like this:</div>
          <div><br>
          </div>
          <div>
            <div
style="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="color:rgb(156,220,254)">dirpath</span> <span
            style="color:rgb(212,212,212)">=</span> <span
            style="color:rgb(78,201,176)">Path</span>(<span
            style="color:rgb(206,145,120)">'C:/Users/sbardos/AppData/Local/Temp/gen_py/3.10/'</span>)</div><div><span
            style="color:rgb(197,134,192)">if</span> <span
            style="color:rgb(156,220,254)">dirpath</span>.<span
            style="color:rgb(220,220,170)">exists</span>() <span
            style="color:rgb(86,156,214)">and</span> <span
            style="color:rgb(156,220,254)">dirpath</span>.<span
            style="color:rgb(220,220,170)">is_dir</span>():</div><div>    <span
            style="color:rgb(78,201,176)">shutil</span>.<span
            style="color:rgb(156,220,254)">rmtree</span>(<span
            style="color:rgb(156,220,254)">dirpath</span>)</div></div>
          </div>
          <div><br>
          </div>
          <div>
            <div style="line-height:19px">
              <div
style="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)"><span
              style="color:rgb(156,220,254)">app</span> <span
              style="color:rgb(212,212,212)">=</span> <span
              style="color:rgb(220,220,170)">Dispatch</span>(<span
              style="color:rgb(206,145,120)">"CT.Application"</span>)</div>
              <div
style="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)"><span
              style="color:rgb(156,220,254)">job</span> <span
              style="color:rgb(212,212,212)">=</span> <span
              style="color:rgb(156,220,254)">app</span>.CreateJobObject()</div>
              <br>
              <div
style="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)"><span
              style="color:rgb(156,220,254)">start</span> <span
              style="color:rgb(212,212,212)">=</span> <span
              style="color:rgb(156,220,254)">timer</span>()</div>
              <br>
              <div
style="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)"><span
              style="color:rgb(197,134,192)">for</span> <span
              style="color:rgb(156,220,254)">i</span> <span
              style="color:rgb(197,134,192)">in</span> <span
              style="color:rgb(78,201,176)">range</span>(<span
              style="color:rgb(181,206,168)">2000</span>):</div>
              <div
style="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)">    <span
              style="color:rgb(156,220,254)">cnt</span>, <span
              style="color:rgb(156,220,254)">devIds</span> <span
              style="color:rgb(212,212,212)">=</span> <span
              style="color:rgb(156,220,254)">job</span>.GetAllDeviceIds()</div>
              <div
style="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)">    <span
              style="color:rgb(156,220,254)">cnt</span>, <span
              style="color:rgb(156,220,254)">sheetIds</span> <span
              style="color:rgb(212,212,212)">=</span> <span
              style="color:rgb(156,220,254)">job</span>.GetSheetIds()</div>
              <div
style="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)">    <span
              style="color:rgb(156,220,254)">dev</span> <span
              style="color:rgb(212,212,212)">=</span> <span
              style="color:rgb(156,220,254)">job</span>.CreateDeviceObject()</div>
              <br>
              <div
style="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)"><span
              style="color:rgb(156,220,254)">end</span> <span
              style="color:rgb(212,212,212)">=</span> <span
              style="color:rgb(156,220,254)">timer</span>()</div>
              <div
style="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)"><span
              style="color:rgb(220,220,170)">print</span>(<span
              style="color:rgb(86,156,214)">f</span><span
              style="color:rgb(206,145,120)">"Time ellapsed (late): </span><span
              style="color:rgb(86,156,214)">{</span><span
              style="color:rgb(156,220,254)">end</span> <span
              style="color:rgb(212,212,212)">-</span> <span
              style="color:rgb(156,220,254)">start</span><span
              style="color:rgb(86,156,214)">}</span><span
              style="color:rgb(206,145,120)">s"</span>)</div>
              <div
style="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)">
</div>
              <div
style="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)">and the ensure Dispatch version:</div>
              <div
style="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)">
</div>
              <div style="">
                <div
style="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);line-height:19px"><div><span
                style="color:rgb(156,220,254)">app</span> <span
                style="color:rgb(212,212,212)">=</span> <span
                style="color:rgb(220,220,170)">EnsureDispatch</span>(<span
                style="color:rgb(206,145,120)">"CT.Application"</span>)</div><div><span
                style="color:rgb(156,220,254)">job</span> <span
                style="color:rgb(212,212,212)">=</span> <span
                style="color:rgb(156,220,254)">app</span>.CreateJobObject()</div>
<div><span style="color:rgb(156,220,254)">start</span> <span
                style="color:rgb(212,212,212)">=</span> <span
                style="color:rgb(156,220,254)">timer</span>()</div>
<div><span style="color:rgb(197,134,192)">for</span> <span
                style="color:rgb(156,220,254)">i</span> <span
                style="color:rgb(197,134,192)">in</span> <span
                style="color:rgb(78,201,176)">range</span>(<span
                style="color:rgb(181,206,168)">2000</span>):</div><div>    <span
                style="color:rgb(156,220,254)">cnt</span>, <span
                style="color:rgb(156,220,254)">devIds</span> <span
                style="color:rgb(212,212,212)">=</span> <span
                style="color:rgb(156,220,254)">job</span>.GetAllDeviceIds(<span
                style="color:rgb(86,156,214)">None</span>)</div><div>    <span
                style="color:rgb(156,220,254)">cnt</span>, <span
                style="color:rgb(156,220,254)">sheetIds</span> <span
                style="color:rgb(212,212,212)">=</span> <span
                style="color:rgb(156,220,254)">job</span>.GetSheetIds(<span
                style="color:rgb(86,156,214)">None</span>)</div><div>    <span
                style="color:rgb(156,220,254)">dev</span> <span
                style="color:rgb(212,212,212)">=</span> <span
                style="color:rgb(156,220,254)">job</span>.CreateDeviceObject()</div>
<div><span style="color:rgb(156,220,254)">end</span> <span
                style="color:rgb(212,212,212)">=</span> <span
                style="color:rgb(156,220,254)">timer</span>()</div><div><span
                style="color:rgb(220,220,170)">print</span>(<span
                style="color:rgb(86,156,214)">f</span><span
                style="color:rgb(206,145,120)">"Time ellapsed (early): </span><span
                style="color:rgb(86,156,214)">{</span><span
                style="color:rgb(156,220,254)">end</span> <span
                style="color:rgb(212,212,212)">-</span> <span
                style="color:rgb(156,220,254)">start</span><span
                style="color:rgb(86,156,214)">}</span><span
                style="color:rgb(206,145,120)">s"</span>)</div><div>
</div><div>
</div></div>
                EnsureDispatch is a little bit slower ~4.2s compared to
                ~4.0s.<br>
                If I don't get a performance boost with EnsureDispatch,
                is there even a point using it?<br>
                <br>
                Thanks,<br>
                Sven<br>
              </div>
            </div>
          </div>
        </div>
      </div>
      <br>
      <fieldset class="moz-mime-attachment-header"></fieldset>
      <pre wrap="" class="moz-quote-pre">_______________________________________________
python-win32 mailing list
<a class="moz-txt-link-abbreviated" href="mailto:[email protected]">[email protected]</a>
<a class="moz-txt-link-freetext" href="https://mail.python.org/mailman/listinfo/python-win32">https://mail.python.org/mailman/listinfo/python-win32</a>
</pre>
    </blockquote>
  </body>
</html>

--------------rVYnIzB20VvkyUvW8n8070BM--

--===============7762019196568212731==
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

--===============7762019196568212731==--