Home
Monday, July 24, 2017
10:52:57 PM
Users online: 0   You are here >> Home > Programming

Forums | Programming Forums search
Forum FAQ
   
  1  
Speed comparison between C and Python
Comrade 
31/8/08 6:15:10 PM
Serf

Hi, I am trying to make a decision as to where to put some of my code.
The code consists of just a few loops and conditional branches, does C do these faster than Python or the other way around?
Any help is appreciated, thanks.

-----
Intel C2Q 6600,Asus P5K-pro,4GB DDR2 RAM,AsusG8800GT, WindowsXP Pro, Ubuntu7.10

freespace 
31/8/08 8:32:22 PM
Hero
Titan


Python is never faster than C.

-----
By perseverance the snail reached the ark.

http://www.shuningbian.net - blog
http://anonshare.pictorii.com - share files anonymously
http://dailydiscovery.b3ta.org - learn something new
its f reespace damn it!

spielentwickler 
31/8/08 10:20:17 PM
Guru

It sounds simple enough, write it in both and see which one is faster...

-----
http://www.last.fm/user/spielentwickler/

Comrade 
31/8/08 10:25:22 PM
Serf

YE that is a good idea, but I am a little lazy lol.
Is Python considered a fast scripting language?
I am using Ctype wrapper, is there a lot of overhead for that?

-----
Intel C2Q 6600,Asus P5K-pro,4GB DDR2 RAM,AsusG8800GT, WindowsXP Pro, Ubuntu7.10

Meeko 
1/9/08 12:31:43 AM
Titan

From what I hear, Python is very slow compared to C. I've never done a comparison myself, though.

But, really, speed doesn't matter that much today - it depends on what you are using it for. For most tasks, Python is easier to use and the speed sacrifice isn't really a big deal.

-----

Penniless_Prophet 
1/9/08 11:27:29 AM
Guru

google it but yes the answer is nothing is faster than C (other than assembler and machine language :-))

C is king of speed because it forces you to manage every little facet yourself.

-----
http://slaveofheaven.blogspot.com


I make magic happen on a regular basis, I am so good at it that people have started to think its normal...

spielentwickler 
1/9/08 11:48:19 AM
Guru

Quote by Penniless_Prophet
google it but yes the answer is nothing is faster than C (other than assembler and machine language :-))

C is king of speed because it forces you to manage every little facet yourself.



Unless you have a really good understanding of the processor you are developing for, C will almost always end up being faster than assembly, as a C compiler will usually build much better optimised code than you can in assembly.

-----
http://www.last.fm/user/spielentwickler/

freespace 
1/9/08 12:29:48 PM
Hero
Titan


Python is considered slow. I have done some scientific computing with python. It is fast to write, but slow to run.

-----
By perseverance the snail reached the ark.

http://www.shuningbian.net - blog
http://anonshare.pictorii.com - share files anonymously
http://dailydiscovery.b3ta.org - learn something new
its f reespace damn it!

pwarren 
1/9/08 1:00:04 PM
Champion

the fun thing with Python is that you can write your modules in C, gaining the performance benefits of C, and keeping the convenience of Python programming.

Of course, you still have the overheads of garbage collection and function calling stuff.

have a look at cElementTree for a good example of c-ification of python modules:
http://effbot.org/zone/celementtree.htm

-----
“Money is a powerful aphrodisiac. But flowers work almost as well.”
-- Lazarus Long

boyter 
1/9/08 1:25:07 PM
Guru

C is probably going to be faster... unless the task is really IO bound then they might of a similar speed.

-----
http://www.boyter.org

kikz 
1/9/08 7:33:24 PM
Immortal

lol lol lol while c is faster than python, only a drongo would say only asm is faster

-----
Q6600@3.2Ghz | 8Gb PC6400 | 2 x 500Gb RAID 0 + 2 x 320 Gb RAID 0 | 19" Benq FP591 + 24" Samsung 245B + 19" Dell | 8800GTS 640 Mb + 8400GS 256 Mb | Gigabyte GA-P35-DS3P | Antec P182 | Corsair HX-620 | Thermalright 120 Extreme | Vista x64

Linux_Inside V2 
1/9/08 8:50:21 PM
Immortal

Quote by kikz
lol lol lol while c is faster than python, only a drongo would say only asm is faster



Wouldn't it have been back in the old days? (like on a Motorola 68k)?

-----

eckythump 
1/9/08 10:25:24 PM
Champion

Whether it be C or ASM source, you still need to compile it into a binary, and it's the C compilers and their cleverness that will almost always produce more efficient optimised code than most people would get if coding directly in ASM.

But yes, back in the ye olde Amiga days, I wouldn't have expected the avilable compilers to have done much in the way of optimisation. All those crazy scandanavian demo programmers worked in ASM.

But you also need to consider that those crazy types were also dealing with very slow hardware, and would spend a lot of time tweaking their code to try and milk every last bit of performance out of it. There's some pretty amazing programs out there, a lot of them by demo programmers who enjoy the challenge of pushing things to their very limits.

-----
"Grandfather had an accident, he got burnt." "Oh no, how bad?" "Well, they don't fuck around at the crematorium."

kikz 
1/9/08 10:52:16 PM
Immortal

certain tasks such as test processing (ie regex) is faster in perl than c.

-----
Q6600@3.2Ghz | 8Gb PC6400 | 2 x 500Gb RAID 0 + 2 x 320 Gb RAID 0 | 19" Benq FP591 + 24" Samsung 245B + 19" Dell | 8800GTS 640 Mb + 8400GS 256 Mb | Gigabyte GA-P35-DS3P | Antec P182 | Corsair HX-620 | Thermalright 120 Extreme | Vista x64

freespace 
1/9/08 11:10:34 PM
Hero
Titan


Faster to write, but I don't see how they can be faster than C, since those libraries are probably C-backed...

-----
By perseverance the snail reached the ark.

http://www.shuningbian.net - blog
http://anonshare.pictorii.com - share files anonymously
http://dailydiscovery.b3ta.org - learn something new
its f reespace damn it!

hetman 
5/9/08 12:20:36 AM
Hero
Titan


Python runs on a bytecode VM so I expect things like loops and other control structures won't actually be noteceably slower in a lot of cases. Once you start doing function calls and data manipulation the speed really starts to take a hit.

The question you really need to be asking is... will I notice Python is slower than C? And as has been mentioned, you can profile the code to find the bottlenecks and convert those bits to C. If you really need to.

The answer really depends on what exactly you're trying to do with it.

-----
Wystarczy tego medrkowania.

ni 
9/9/08 8:45:36 PM
Hero
Guru


kikz, can you cite an example or study to back that claim up? Thanks.

-----
perl -e 'sub _{print chr pop;$-+=pop@a}_(_(_(_(_(_(_(_(_
(_(_(_(_(_(_(_(_(_(_(_(_(_(_(_(_(_(@a=(-104,13,
-6,8,2,25,40,-76,-6,13,21,48,-82,13,-3,-12,5,1,
45,33,-84,1,-2,43,74),0))))))))))))))))))))))))))'

boyter 
9/9/08 9:03:25 PM
Guru

A ten second search turned up this

http://stuffivelearned.org/doku.php?id=programming:general:phpvspythonvsperl

I don't really like benchmarks really, but it does show that perl is faster then c in some regex tests.

-----
http://www.boyter.org

TheSecret 
9/9/08 11:25:06 PM
Overlord
Quote by boyter
A ten second search turned up this

http://stuffivelearned.org/doku.php?id=programming:general:phpvspythonvsperl

I don't really like benchmarks really, but it does show that perl is faster then c in some regex tests.



You mean python, right?

-----
Part of the inhumanity of the computer is that, once it is competently programmed and working smoothly, it is completely honest.

freespace 
10/9/08 2:29:14 AM
Hero
Titan


I am with TheSecret - did you paste the wrong link? That article has nothing to do with C, except for the fact it uses it as part of the English language more than 100 times.

-----
By perseverance the snail reached the ark.

http://www.shuningbian.net - blog
http://anonshare.pictorii.com - share files anonymously
http://dailydiscovery.b3ta.org - learn something new
its f reespace damn it!

  1  
Forums | Programming