Re: C is it faster than numpy

Edward Hartley <[email protected]> Fri, 25 Feb 2022 09:57:44 +0000
Newsgroups gmane.comp.python.org.uk
Message-ID <[email protected]>
--===============2430412059090833161==
Content-Type: multipart/alternative; boundary=Apple-Mail-92055015-CFC3-48FD-8F33-7E28131E888A
Content-Transfer-Encoding: 7bit


--Apple-Mail-92055015-CFC3-48FD-8F33-7E28131E888A
Content-Type: text/plain;
	charset=utf-8
Content-Transfer-Encoding: quoted-printable


Hi,=20
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 thin P=
ython wrapper. The docs were written circa =E2=80=9898 by the original libra=
ry 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 a=
voiding unnecessary memory allocation.=20
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:
>=20
> =EF=BB=BF
> Well,
> numpy is written in C :) Maybe your C is not the numpy equivalent?
> Best Regards,
> Giorgio
>=20
>> Il giorno ven 25 feb 2022 alle ore 09:03 BELAHCENE Abdelkader <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=20
>> Regards
>> Numpy implements vectorization for arrays, or I'm wrong. Anyway here is a=
n example Let's look at the following case:
>> Here is the result on my laptop i3:
>>=20
>> Labs$ python3 tempsExe.py  50000=20
>>   sum with Python: 1250025000 and NumPy 1250025000
>>       time used Python Sum:  37.28 sec=20
>>       time used  Numpy Sum:  1.85 sec
>>=20
>> Labs$ ./tt    50000=20
>>     CPU  time :7.521730
>>     The value : 1250025000=20
>> --------------------------------------------
>>=20
>> This is the Python3 program :
>>=20
>> import timeit as it
>> import numpy as np
>> import sys
>> try :
>> n=3Deval(sys.argv[1])
>> except:
>> print ("needs integer as argument") ; exit()
>>=20
>> a=3Drange(1,n+1)
>> b=3Dnp.array(a)
>> def func1():     return sum(a)
>> def func2(): return np.sum(b)
>>=20
>> 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")
>>=20
>> 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;=20
>>         long v,count; int n;
>>        if ( argc < 2) {
>>               printf("Please give an argument");
>>              return -1;
>>       }
>>     n=3Datoi(argv[1]);=20
>>     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
>=20
>=20
> --=20
> Life is a chess game - Anonymous.
> _______________________________________________
> python-uk mailing list
> [email protected]
> https://mail.python.org/mailman/listinfo/python-uk

--Apple-Mail-92055015-CFC3-48FD-8F33-7E28131E888A
Content-Type: text/html;
	charset=utf-8
Content-Transfer-Encoding: quoted-printable

<html><head><meta http-equiv=3D"content-type" content=3D"text/html; charset=3D=
utf-8"></head><body dir=3D"auto"><div dir=3D"ltr"></div><div dir=3D"ltr"><br=
></div><div dir=3D"ltr">Hi,&nbsp;</div><div dir=3D"ltr">Simply check the ori=
ginal Numpy aka Numerical Python docs where it=E2=80=99s comprehensively exp=
lained that the library is implemented in C with a thin Python wrapper. The d=
ocs were written circa =E2=80=9898 by the original library author. The libra=
ry 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 k=
ey is the effort given to optimisation, and care with avoiding unnecessary m=
emory allocation.&nbsp;</div><div dir=3D"ltr">I=E2=80=99ll dig out the refer=
ence when I=E2=80=99ve tracked it down.</div><div dir=3D"ltr">Best of Luck</=
div><div dir=3D"ltr">Ed Hartley</div><div dir=3D"ltr"><br><blockquote type=3D=
"cite">On 25 Feb 2022, at 09:42, Giorgio Zoppi &lt;[email protected]&g=
t; wrote:<br><br></blockquote></div><blockquote type=3D"cite"><div dir=3D"lt=
r">=EF=BB=BF<div dir=3D"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=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:[email protected]">[email protected]</a>&gt; h=
a scritto:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0px 0p=
x 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div di=
r=3D"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 i=
s 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>T=
hanks 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 i=
s the result on my laptop i3:<br><br>Labs$ <b>python3 tempsExe.py&nbsp; 5000=
0</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<b=
r>&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=3Deval(sys.argv[1=
])<br>except:<br>	print ("needs integer as argument") ; exit()<br>=
	<br>a=3Drange(1,n+1)<br>b=3Dnp.array(a)<br>def func1(): &nbsp; &nbsp; retur=
n sum(a)<br>def func2():	 return np.sum(b)<br><br>print(f"sum with P=
ython: {func1()} and NumPy {func2()} ")<br>tm1=3Dit.timeit(stmt=3Dfunc1, num=
ber=3Dn)<br>print(f"time used Python Sum: {round(tm1,2)} sec")<br>tm2=3Dit.t=
imeit(stmt=3Dfunc2, number=3Dn)<br>print(f"time used &nbsp;Numpy Sum: {round=
(tm2,2)} sec")<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=3D0;<br>&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int &nbsp;i=3D1; i&lt;=3D n;i++=
) r+=3D 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;&nbs=
p;&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("Please give an argument");<br>&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return -1;<br>&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; n=3Datoi(argv[1]); <br>&nbsp;&nbsp;=
&nbsp; c0 =3D clock();<br>&nbsp;&nbsp;&nbsp;&nbsp; <b>for (int j=3D0;j &lt; n=
;j++)	v=3Dfunc1(n);</b><br>&nbsp;&nbsp;&nbsp;&nbsp; c1 =3D clock();<br>&n=
bsp;&nbsp;&nbsp;&nbsp; printf ("\tCPU &nbsp;time :%.2f sec", (float)(c1 - c0=
)/CLOCKS_PER_SEC);<br>&nbsp;&nbsp;&nbsp;&nbsp; printf("\n\tThe value : %ld\n=
", &nbsp;v);<br>}<br></div>
_______________________________________________<br>
python-uk mailing list<br>
<a href=3D"mailto:[email protected]" target=3D"_blank">[email protected]=
rg</a><br>
<a href=3D"https://mail.python.org/mailman/listinfo/python-uk" rel=3D"norefe=
rrer" 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" c=
lass=3D"gmail_signature"><div dir=3D"ltr">Life is a chess game - Anonymous.<=
br></div></div>
<span>_______________________________________________</span><br><span>python=
-uk mailing list</span><br><span>[email protected]</span><br><span>https:=
//mail.python.org/mailman/listinfo/python-uk</span><br></div></blockquote></=
body></html>=

--Apple-Mail-92055015-CFC3-48FD-8F33-7E28131E888A--

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

--===============2430412059090833161==--