ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi, every body
I write 2 programs : in C and Python to compute the time consumed.
As expected the compile C program ran very fastly compared to Python.
But I am suprised and shocked by the result from Numpy!!!, numpy faster than C.
May be the compared procedure is not correct!!
or error in comparison
Here are my programs and results
The C program the func1 is called N times
given as argument
#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("Number of execution as 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);
}
----------------------------------------------------
Python3 Program executed same number times as C
import timeit as it
import numpy as np
import sys
try :
n=eval(sys.argv[1])
except:
print ("Number of execution as argument");
exit()
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")
'''
Example of execution
./timeC 50000
CPU time :7.66 sec ----------> Consumed by C program
The value : 1250025000
python3 timeExe.py 50000
sum with Python: 1250025000 and NumPy 1250025000
time used Python Sum: 33.0 sec ----------> Consumed by Python
time used Numpy Sum: 1.13 sec ----------> Consumed by Numpy module
'''
Thanks a lot for help or any site for more professional site.
Please post your thread only once and in only one forum. Posting a single thread in the most relevant forum will make it easier for members to help you and will keep the discussion in one place. This thread is being closed because it is a duplicate.
Please continue the discussion in the original thread which has received multiple helpful replies.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.