Re: C is it faster than numpy

Adam Johnson via python-uk <[email protected]> Fri, 25 Feb 2022 10:02:09 +0000
Newsgroups gmane.comp.python.org.uk
Message-ID <CAMyDDM3U1jVoUiRdj02eoTDqm4k8Ugy22PJ3vckYuy8wkEMr6g@mail.gmail.com>
--===============7997855561868589294==
Content-Type: multipart/alternative; boundary="00000000000074085005d8d4cb55"

--00000000000074085005d8d4cb55
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Also interestingly, you can see the language breakdown on the right hand
side of https://github.com/numpy/numpy/ :

Python 62.5%
C 35.3%
C++ 1.0%
Cython 0.9%
Shell 0.2%
Fortran 0.1%

Much of the Python code is tests.

On Fri, Feb 25, 2022 at 9:57 AM Edward Hartley <[email protected]> wrote=
:

>
> Hi,
> Simply check the original Numpy aka Numerical Python docs where it=E2=80=
=99s
> comprehensively explained that the library is implemented in C with a thi=
n
> Python wrapper. The docs were written circa =E2=80=9898 by the original l=
ibrary
> author. The library was optimised over a long period before being release=
d.
> I=E2=80=99ve seen similar results when replacing an optimised Fortran lib=
rary with
> C/C++ the key is the effort given to optimisation, and care with avoiding
> unnecessary memory allocation.
> I=E2=80=99ll dig out the reference when I=E2=80=99ve tracked it down.
> Best of Luck
> Ed Hartley
>
> On 25 Feb 2022, at 09:42, Giorgio Zoppi <[email protected]> wrote:
>
> =EF=BB=BF
> 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 th=
an
>> 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=3Deval(sys.argv[1])
>> except:
>> print ("needs integer as argument") ; exit()
>>
>> a=3Drange(1,n+1)
>> b=3Dnp.array(a)
>> def func1():     return sum(a)
>> def func2(): return np.sum(b)
>>
>> print(f"sum with Python: {func1()} and NumPy {func2()} ")
>> tm1=3Dit.timeit(stmt=3Dfunc1, number=3Dn)
>> print(f"time used Python Sum: {round(tm1,2)} sec")
>> tm2=3Dit.timeit(stmt=3Dfunc2, number=3Dn)
>> 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=3D0;
>>         for (int  i=3D1; i<=3D n;i++) r+=3D 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=3Datoi(argv[1]);
>>     c0 =3D clock();
>>      *for (int j=3D0;j < n;j++) v=3Dfunc1(n);*
>>      c1 =3D 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
>
> _______________________________________________
> python-uk mailing list
> [email protected]
> https://mail.python.org/mailman/listinfo/python-uk
>

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

<div dir=3D"ltr">Also interestingly, you can see the language breakdown on =
the right hand side of <a href=3D"https://github.com/numpy/numpy/">https://=
github.com/numpy/numpy/</a> :<br><br>Python 62.5%<br>C 35.3%<br>C++ 1.0%<br=
>Cython 0.9%<br>Shell 0.2%<br>Fortran 0.1%<br><br>Much of the Python code i=
s tests.<br></div><br><div class=3D"gmail_quote"><div dir=3D"ltr" class=3D"=
gmail_attr">On Fri, Feb 25, 2022 at 9:57 AM Edward Hartley &lt;<a href=3D"m=
ailto:[email protected]">[email protected]</a>&gt; wrote:<br></div><b=
lockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-le=
ft:1px solid rgb(204,204,204);padding-left:1ex"><div dir=3D"auto"><div dir=
=3D"ltr"></div><div dir=3D"ltr"><br></div><div dir=3D"ltr">Hi,=C2=A0</div><=
div dir=3D"ltr">Simply check the original Numpy aka Numerical Python docs w=
here it=E2=80=99s comprehensively explained that the library is implemented=
 in C with a thin Python wrapper. The docs were written circa =E2=80=9898 b=
y the original library author. The library was optimised over a long period=
 before being released. I=E2=80=99ve seen similar results when replacing an=
 optimised Fortran library with C/C++ the key is the effort given to optimi=
sation, and care with avoiding unnecessary memory allocation.=C2=A0</div><d=
iv dir=3D"ltr">I=E2=80=99ll dig out the reference when I=E2=80=99ve tracked=
 it down.</div><div dir=3D"ltr">Best of Luck</div><div dir=3D"ltr">Ed Hartl=
ey</div><div dir=3D"ltr"><br><blockquote type=3D"cite">On 25 Feb 2022, at 0=
9:42, Giorgio Zoppi &lt;<a href=3D"mailto:[email protected]" target=
=3D"_blank">[email protected]</a>&gt; wrote:<br><br></blockquote></di=
v><blockquote type=3D"cite"><div dir=3D"ltr">=EF=BB=BF<div dir=3D"ltr">Well=
,<div>numpy is written in C :) Maybe your C is not the numpy equivalent?</d=
iv><div>Best Regards,</div><div>Giorgio</div></div><br><div class=3D"gmail_=
quote"><div dir=3D"ltr" class=3D"gmail_attr">Il giorno ven 25 feb 2022 alle=
 ore 09:03 BELAHCENE Abdelkader &lt;<a href=3D"mailto:abdelkader.belahcene@=
enst.dz" target=3D"_blank">[email protected]</a>&gt; ha scritto:=
<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.8=
ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir=3D"ltr=
">Hi,<br>a lot of people think that C (or C++) is faster than python, yes I=
 agree, but I think that&#39;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 =C2=A0a doc speaking =C2=A0about the subject?<br></b>=
Thanks a lot <br>Regards<br>Numpy implements vectorization for arrays, or I=
&#39;m wrong. Anyway here is an example Let&#39;s look at the following cas=
e:<br>Here is the result on my laptop i3:<br><br>Labs$ <b>python3 tempsExe.=
py=C2=A0 50000</b> <br>=C2=A0 sum with Python: 1250025000 and NumPy 1250025=
000<br>=C2=A0 =C2=A0 =C2=A0 time used Python Sum:=C2=A0<b> 37.28 sec </b><b=
r>=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=A0ti=
me :7.521730<br>=C2=A0 =C2=A0 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=3Dev=
al(sys.argv[1])<br>except:<br>	print (&quot;needs integer as argument&quot;=
) ; exit()<br>	<br>a=3Drange(1,n+1)<br>b=3Dnp.array(a)<br>def func1(): =C2=
=A0 =C2=A0 return sum(a)<br>def func2():	 return np.sum(b)<br><br>print(f&q=
uot;sum with Python: {func1()} and NumPy {func2()} &quot;)<br>tm1=3Dit.time=
it(stmt=3Dfunc1, number=3Dn)<br>print(f&quot;time used Python Sum: {round(t=
m1,2)} sec&quot;)<br>tm2=3Dit.timeit(stmt=3Dfunc2, number=3Dn)<br>print(f&q=
uot;time used =C2=A0Numpy Sum: {round(tm2,2)} sec&quot;)<br><br>and Here th=
e C program:<br>#include &lt;time.h&gt;<br>#include &lt;stdio.h&gt;<br>#inc=
lude &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 main(int argc, c=
har* argv[]){<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 clock_t c=
0, 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 clock();<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 printf(&quot;\n\tThe =
value : %ld\n&quot;, =C2=A0v);<br>}<br></div>
_______________________________________________<br>
python-uk mailing list<br>
<a href=3D"mailto:[email protected]" target=3D"_blank">python-uk@python.=
org</a><br>
<a href=3D"https://mail.python.org/mailman/listinfo/python-uk" rel=3D"noref=
errer" target=3D"_blank">https://mail.python.org/mailman/listinfo/python-uk=
</a><br>
</blockquote></div><br clear=3D"all"><div><br></div>-- <br><div dir=3D"ltr"=
><div dir=3D"ltr">Life is a chess game - Anonymous.<br></div></div>
<span>_______________________________________________</span><br><span>pytho=
n-uk mailing list</span><br><span><a href=3D"mailto:[email protected]" t=
arget=3D"_blank">[email protected]</a></span><br><span><a href=3D"https:=
//mail.python.org/mailman/listinfo/python-uk" target=3D"_blank">https://mai=
l.python.org/mailman/listinfo/python-uk</a></span><br></div></blockquote></=
div>_______________________________________________<br>
python-uk mailing list<br>
<a href=3D"mailto:[email protected]" target=3D"_blank">python-uk@python.=
org</a><br>
<a href=3D"https://mail.python.org/mailman/listinfo/python-uk" rel=3D"noref=
errer" target=3D"_blank">https://mail.python.org/mailman/listinfo/python-uk=
</a><br>
</blockquote></div>

--00000000000074085005d8d4cb55--

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

--===============7997855561868589294==--