On Sat, 26 Feb 2022 at 03:13, BELAHCENE Abdelkader <abdelkader.belahc...@enst.dz> wrote: > *This is the Python3 program :import timeit as itimport numpy as npimport > systry : 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")*
This is terrible code. Even aside from the messed-up formatting (for which your mail client is probably to blame), using eval() and a bare "except:" clause is not a good way to use Python. And then you get timeit to do as many iterations as the length of the array, which is hardly indicative or meaningful for small values. > *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);}* At least you're consistent, using an iteration count equal to the length of the array again. But that just means that it's equally meaningless. Did you know that Python's timeit and C's clock don't even measure the same thing? ChrisA -- https://mail.python.org/mailman/listinfo/python-list