Re: C is it faster than numpy

SW <[email protected]> Fri, 25 Feb 2022 10:53:20 +0100
Newsgroups gmane.comp.python.org.uk
Message-ID <AS8P193MB2013EBFD502337BA1DB2FD31B83E9@AS8P193MB2013.EURP193.PROD.OUTLOOK.COM>
--===============7155318232171287642==
Content-Type: multipart/alternative;
 boundary="------------pTTKRAiqvzMFiR6HQdylIH4V"
Content-Language: en-US

--------------pTTKRAiqvzMFiR6HQdylIH4V
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit

I think it uses fortran (or used to?) but yes, still close to the metal.

Thanks,
S

On 25/02/2022 10:41, Giorgio Zoppi wrote:
> Well,
> numpy is written in C :) Maybe your C is not the numpy equivalent?
> Best Regards,
> Giorgio
>
> Il giorno ven 25 feb 2022 alle ore 09:03 BELAHCENE Abdelkader 
> <[email protected]> ha scritto:
>
>     Hi,
>     a lot of people think that C (or C++) is faster than python, yes I
>     agree, but I think that's not the case with numpy, I believe numpy
>     is faster than C, at least in some cases.
>     *Is there another explanation ?
>     Or where can find  a doc speaking  about the subject?
>     *Thanks a lot
>     Regards
>     Numpy implements vectorization for arrays, or I'm wrong. Anyway
>     here is an example Let's look at the following case:
>     Here is the result on my laptop i3:
>
>     Labs$ *python3 tempsExe.py  50000*
>       sum with Python: 1250025000 and NumPy 1250025000
>           time used Python Sum: *37.28 sec *
>           time used  Numpy Sum: *1.85 sec*
>
>     Labs$ *./tt    50000 *
>     *  CPU  time :7.521730
>         The value : 1250025000
>     *--------------------------------------------
>
>     This is the Python3 program :
>
>     import timeit as it
>     import numpy as np
>     import sys
>     try :
>     n=eval(sys.argv[1])
>     except:
>     print ("needs integer as argument") ; exit()
>
>     a=range(1,n+1)
>     b=np.array(a)
>     def func1():     return sum(a)
>     def func2(): return np.sum(b)
>
>     print(f"sum with Python: {func1()} and NumPy {func2()} ")
>     tm1=it.timeit(stmt=func1, number=n)
>     print(f"time used Python Sum: {round(tm1,2)} sec")
>     tm2=it.timeit(stmt=func2, number=n)
>     print(f"time used  Numpy Sum: {round(tm2,2)} sec")
>
>     and Here the C program:
>     #include <time.h>
>     #include <stdio.h>
>     #include <stdlib.h>
>     long func1(int n){
>              long  r=0;
>             for (int  i=1; i<= n;i++) r+= i;
>              return r;
>     }
>     int main(int argc, char* argv[]){
>              clock_t c0, c1;
>             long v,count; int n;
>            if ( argc < 2) {
>                   printf("Please give an argument");
>                  return -1;
>           }
>         n=atoi(argv[1]);
>         c0 = clock();
>     *for (int j=0;j < n;j++) v=func1(n);*
>          c1 = clock();
>          printf ("\tCPU  time :%.2f sec", (float)(c1 -
>     c0)/CLOCKS_PER_SEC);
>          printf("\n\tThe value : %ld\n",  v);
>     }
>     _______________________________________________
>     python-uk mailing list
>     [email protected]
>     https://mail.python.org/mailman/listinfo/python-uk
>
>
>
> -- 
> Life is a chess game - Anonymous.
>
> _______________________________________________
> python-uk mailing list
> [email protected]
> https://mail.python.org/mailman/listinfo/python-uk

--------------pTTKRAiqvzMFiR6HQdylIH4V
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 8bit

<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body>
    <div class="moz-cite-prefix">I think it uses fortran (or used to?)
      but yes, still close to the metal.<br>
      <br>
      Thanks,<br>
      S<br>
      <br>
      On 25/02/2022 10:41, Giorgio Zoppi wrote:<br>
    </div>
    <blockquote type="cite" cite="mid:CAHW5HkoQ=9Bvcm7L3CejpMedFezUf+ni+7NDms_RLeyq-VSnzg@mail.gmail.com">
      
      <div dir="ltr">Well,
        <div>numpy is written in C :) Maybe your C is not the numpy
          equivalent?</div>
        <div>Best Regards,</div>
        <div>Giorgio</div>
      </div>
      <br>
      <div class="gmail_quote">
        <div dir="ltr" class="gmail_attr">Il giorno ven 25 feb 2022 alle
          ore 09:03 BELAHCENE Abdelkader &lt;<a href="mailto:[email protected]" moz-do-not-send="true" class="moz-txt-link-freetext">[email protected]</a>&gt;
          ha scritto:<br>
        </div>
        <blockquote class="gmail_quote" style="margin:0px 0px 0px
          0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
          <div dir="ltr">Hi,<br>
            a lot of people think that C (or C++) is faster than python,
            yes I agree, but I think that's not the case with numpy, I
            believe numpy is faster than C, at least in some cases.<br>
            <b>Is there another explanation ?<br>
              Or where can find &nbsp;a doc speaking &nbsp;about the subject?<br>
            </b>Thanks a lot <br>
            Regards<br>
            Numpy implements vectorization for arrays, or I'm wrong.
            Anyway here is an example Let's look at the following case:<br>
            Here is the result on my laptop i3:<br>
            <br>
            Labs$ <b>python3 tempsExe.py&nbsp; 50000</b> <br>
            &nbsp; sum with Python: 1250025000 and NumPy 1250025000<br>
            &nbsp; &nbsp; &nbsp; time used Python Sum:&nbsp;<b> 37.28 sec </b><br>
            &nbsp; &nbsp; &nbsp; time used &nbsp;Numpy Sum:&nbsp; <b>1.85 sec</b><br>
            <br>
            Labs$ <b>./tt &nbsp;&nbsp; 50000 </b><br>
            &nbsp;<b> &nbsp; CPU &nbsp;time :7.521730<br>
              &nbsp; &nbsp; The value : 1250025000 <br>
            </b>--------------------------------------------<br>
            <br>
            This is the Python3 program :<br>
            <br>
            import timeit as it<br>
            import numpy as np<br>
            import sys<br>
            try :<br>
            n=eval(sys.argv[1])<br>
            except:<br>
            print (&quot;needs integer as argument&quot;) ; exit()<br>
            <br>
            a=range(1,n+1)<br>
            b=np.array(a)<br>
            def func1(): &nbsp; &nbsp; return sum(a)<br>
            def func2(): return np.sum(b)<br>
            <br>
            print(f&quot;sum with Python: {func1()} and NumPy {func2()} &quot;)<br>
            tm1=it.timeit(stmt=func1, number=n)<br>
            print(f&quot;time used Python Sum: {round(tm1,2)} sec&quot;)<br>
            tm2=it.timeit(stmt=func2, number=n)<br>
            print(f&quot;time used &nbsp;Numpy Sum: {round(tm2,2)} sec&quot;)<br>
            <br>
            and Here the C program:<br>
            #include &lt;time.h&gt;<br>
            #include &lt;stdio.h&gt;<br>
            #include &lt;stdlib.h&gt;<br>
            long func1(int n){<br>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; long &nbsp;r=0;<br>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int &nbsp;i=1; i&lt;= n;i++) r+= i;<br>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return r;<br>
            }<br>
            int main(int argc, char* argv[]){<br>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; clock_t c0, c1; <br>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; long v,count; int n;<br>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ( argc &lt; 2) {<br>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;Please give an argument&quot;);<br>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return -1;<br>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>
            &nbsp;&nbsp;&nbsp; n=atoi(argv[1]); <br>
            &nbsp;&nbsp;&nbsp; c0 = clock();<br>
            &nbsp;&nbsp;&nbsp;&nbsp; <b>for (int j=0;j &lt; n;j++) v=func1(n);</b><br>
            &nbsp;&nbsp;&nbsp;&nbsp; c1 = clock();<br>
            &nbsp;&nbsp;&nbsp;&nbsp; printf (&quot;\tCPU &nbsp;time :%.2f sec&quot;, (float)(c1 -
            c0)/CLOCKS_PER_SEC);<br>
            &nbsp;&nbsp;&nbsp;&nbsp; printf(&quot;\n\tThe value : %ld\n&quot;, &nbsp;v);<br>
            }<br>
          </div>
          _______________________________________________<br>
          python-uk mailing list<br>
          <a href="mailto:[email protected]" target="_blank" moz-do-not-send="true" class="moz-txt-link-freetext">[email protected]</a><br>
          <a href="https://mail.python.org/mailman/listinfo/python-uk" rel="noreferrer" target="_blank" moz-do-not-send="true" class="moz-txt-link-freetext">https://mail.python.org/mailman/listinfo/python-uk</a><br>
        </blockquote>
      </div>
      <br clear="all">
      <div><br>
      </div>
      -- <br>
      <div dir="ltr" class="gmail_signature">
        <div dir="ltr">Life is a chess game - Anonymous.<br>
        </div>
      </div>
      <br>
      <fieldset class="moz-mime-attachment-header"></fieldset>
      <pre class="moz-quote-pre" wrap="">_______________________________________________
python-uk 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-uk">https://mail.python.org/mailman/listinfo/python-uk</a>
</pre>
    </blockquote>
    <br>
  </body>
</html>

--------------pTTKRAiqvzMFiR6HQdylIH4V--

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

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

--===============7155318232171287642==--