C is it faster than numpy

BELAHCENE Abdelkader <[email protected]> Fri, 25 Feb 2022 10:03:16 +0100
Newsgroups gmane.comp.python.org.uk
Message-ID <CABcwF5m6u-NWhtnEwG+iopLFPZBJopO7hLP+f3uhwUdzn93wNw@mail.gmail.com>
--===============1140119293646223207==
Content-Type: multipart/alternative; boundary="000000000000d0d6d805d8d3f84b"

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

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);
}

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

<div dir=3D"ltr">Hi,<br>a lot of people think that C (or C++) is faster tha=
n python, yes I agree, but I think that&#39;s not the case with numpy, I be=
lieve numpy is faster than C, at least in some cases.<br><b>Is there anothe=
r explanation ?<br>Or where can find =C2=A0a doc speaking =C2=A0about the s=
ubject?<br></b>Thanks a lot <br>Regards<br>Numpy implements vectorization f=
or arrays, or I&#39;m wrong. Anyway here is an example Let&#39;s look at th=
e following case:<br>Here is the result on my laptop i3:<br><br>Labs$ <b>py=
thon3 tempsExe.py=C2=A0 50000</b> <br>=C2=A0 sum with Python: 1250025000 an=
d NumPy 1250025000<br>=C2=A0 =C2=A0 =C2=A0 time used Python Sum:=C2=A0<b> 3=
7.28 sec </b><br>=C2=A0 =C2=A0 =C2=A0 time used =C2=A0Numpy Sum:=C2=A0 <b>1=
.85 sec</b><br><br>Labs$ <b>./tt =C2=A0=C2=A0 50000 	</b><br>=C2=A0<b> =C2=
=A0 CPU =C2=A0time :7.521730<br>=C2=A0 =C2=A0 The value : 1250025000 <br></=
b>--------------------------------------------<br><br>This is the Python3 p=
rogram :<br><br>import timeit as it<br>import numpy as np<br>import sys<br>=
try :<br>	n=3Deval(sys.argv[1])<br>except:<br>	print (&quot;needs integer a=
s argument&quot;) ; exit()<br>	<br>a=3Drange(1,n+1)<br>b=3Dnp.array(a)<br>d=
ef func1(): =C2=A0 =C2=A0 return sum(a)<br>def func2():	 return np.sum(b)<b=
r><br>print(f&quot;sum with Python: {func1()} and NumPy {func2()} &quot;)<b=
r>tm1=3Dit.timeit(stmt=3Dfunc1, number=3Dn)<br>print(f&quot;time used Pytho=
n Sum: {round(tm1,2)} sec&quot;)<br>tm2=3Dit.timeit(stmt=3Dfunc2, number=3D=
n)<br>print(f&quot;time used =C2=A0Numpy Sum: {round(tm2,2)} sec&quot;)<br>=
<br>and Here the C program:<br>#include &lt;time.h&gt;<br>#include &lt;stdi=
o.h&gt;<br>#include &lt;stdlib.h&gt;<br>long func1(int n){<br>=C2=A0=C2=A0=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 long =C2=A0r=3D0;<br>=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0 for (int =C2=A0i=3D1; i&lt;=3D n;i++) r+=3D i;<=
br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return r;<br>}<br>int m=
ain(int argc, char* argv[]){<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=
=C2=A0 clock_t c0, c1; <br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 long =
v,count; int n;<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 if ( argc &lt; 2) {=
<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0=C2=A0 printf(&quot;Please give an argument&quot;);<br>=C2=A0=C2=A0=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return -1;<br>=C2=
=A0=C2=A0=C2=A0=C2=A0=C2=A0 }<br>=C2=A0=C2=A0=C2=A0 n=3Datoi(argv[1]); <br>=
=C2=A0=C2=A0=C2=A0 c0 =3D clock();<br>=C2=A0=C2=A0=C2=A0=C2=A0 <b>for (int =
j=3D0;j &lt; n;j++)	v=3Dfunc1(n);</b><br>=C2=A0=C2=A0=C2=A0=C2=A0 c1 =3D cl=
ock();<br>=C2=A0=C2=A0=C2=A0=C2=A0 printf (&quot;\tCPU =C2=A0time :%.2f sec=
&quot;, (float)(c1 - c0)/CLOCKS_PER_SEC);<br>=C2=A0=C2=A0=C2=A0=C2=A0 print=
f(&quot;\n\tThe value : %ld\n&quot;, =C2=A0v);<br>}<br></div>

--000000000000d0d6d805d8d3f84b--

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

--===============1140119293646223207==--