LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Closed Thread
  Search this Thread
Old 07-07-2022, 05:14 AM   #1
abd_bela
Member
 
Registered: Dec 2002
Location: algeria
Distribution: redhat 7.3, debian lenny
Posts: 627

Rep: Reputation: 31
Angry compare execution time C, Python and numpy


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()

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")
'''
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.
 
Old 07-07-2022, 05:16 AM   #2
abd_bela
Member
 
Registered: Dec 2002
Location: algeria
Distribution: redhat 7.3, debian lenny
Posts: 627

Original Poster
Rep: Reputation: 31
Hi,
I want to attach a file ,
where to do it
thanks
 
Old 07-07-2022, 05:47 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,880
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Meta: There is a [Go Advanced] button, press it and you will get many options including attaching files.

On: Numpy is C, just compiled with optimalization.
 
Old 07-07-2022, 08:42 AM   #4
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,249

Rep: Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323Reputation: 5323
You already had this thread. Same question and same sample code.

is numpy faster than C ?

I was trying to find you the implementation of numpy.sum. I got this instead, and it says the same thing you were told in the other thread:

Why is numpy faster than my c/c++ code for summing an array of float?

Last edited by dugan; 07-07-2022 at 09:57 AM.
 
Old 07-07-2022, 10:13 AM   #5
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206
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.
 
1 members found this post helpful.
  


Closed Thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Armadillo matrices via NumPy array and Python Extension to C++ LXer Syndicated Linux News 0 07-31-2021 12:56 AM
[SOLVED] Install numpy, scipy, pandas and scikit-learn with python 3 Hasek39 Slackware 9 04-29-2018 01:47 PM
Installing Python package including numpy , scipy,mpi4py , pycxx , symeig madani Linux - Software 1 03-14-2009 06:22 PM
python error , import numpy ufmale Programming 2 08-25-2008 11:59 PM
Visual Python/Numpy uranologist Linux - Software 0 09-28-2003 10:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration