pythonnewbie
pythonnewbie

Reputation: 321

Python vs C : Line of Code Comparison vs Dev Time

Hi I'm currently learning Python since the syntax feels so succinct and the idioms match well with my mental model.

However I'm also interested in learning about OS internals and reverse engineering software, which ultimately means knowing C in a rather thorough capacity.

When originally picking a language I did lots of reading and comparisons, and it seems that a number thrown out a lot is that to write short idiomatic statements in Python would require the equivalent of a few hundred lines of C (I'd guess code for memory management, writing the code for dictionaries,lists etc) that we take for granted as built into the Python language.

1) With an average C programmer, is that 100-200 lines of code per Python idiom anywhere near accurate?

Because C doesn't come built-in with Python-like constructs such as dictionaries/lists(with all their nice methods etc):

2) Do C programmers tend to build these constructs from scratch and then re-use them between projects to greatly reduce the actual amount of hand coding for their projects?

I assume re-using libraries like boost:: stuff also again, reduces some of the boilerplate hand coding also...

3) But does using popular libraries and re-using common code one has written before in C for basic constructs/etc, how much does that revise the lines of code written in C compared to the code in Python of a enthusiast sized code base?

I know specific numbers aren't possible, but is it possible with libraries, code reuse etc, to have a development time in C close to that of Python without being a Linus Torvalds style coding machine?

Thanks!

Upvotes: 3

Views: 8526

Answers (6)

Giorgio
Giorgio

Reputation: 5173

I think Python is more productive for small projects (up to a few thousand lines of code).

On the other hand, C is better suited for large projects (even though IMHO there are better languages for that, such as Ada): static type-checking allows to find many errors at compile time that are much more difficult to detect at run-time, especially in a large program.

In a larger C project, the lack of lists and other powerful data structures that are found in Python can be compensated by implementing or using custom libraries. I agree with user stacker that by using well-designed libraries your C code can be pretty concise.

Upvotes: 1

Russell Borogove
Russell Borogove

Reputation: 19037

Depends greatly on the task and the size of the project. For many small interesting tasks, I would not be surprised by 100:1 smaller Python code simply because the standard libraries are extremely good. If you find, buy, or build C/C++ libraries that do what you want, I imagine the ratio would be much more like 3:1 on big projects.

However, finding, buying, and building C/C++ libraries does take time and effort, so I believe in the vast majority of cases, Python is going to be much faster to develop in.

Upvotes: 0

Paul Rubel
Paul Rubel

Reputation: 27222

It depends.

Try to write an interrupt handler in python. Someone could probably make it work but it's going to be a dancing bear, the dancing is not good but it is surprising that a bear can do it. Want to write an OS or do some embedded programming you're not going to be able to use python. It's telling that the main python implementation is written in C.

That being said I'm amazed at some of the low-level stuff that you can do with python. The high-level stuff is almost a given if you're measuring lines of code. Python is just a higher-level language.

They are both very useful tools, just for different types of projects. Knowing both would be very useful, particularly when you need to interface to some new functionality in python that doesn't yet have a python binding.

For the types of projects most developers work on python is going to be more consice and quicker to write and debug. You may be able to make a library of reusable C code, but a good python programmer will be doing the same thing with their python code, at a higher level.

Upvotes: 1

S.Lott
S.Lott

Reputation: 391854

but is it possible with libraries, code reuse etc, to have a development time in C close to that of Python

No.

You've missed the most important point.

Python's interactive. It's not edit-compile-link-execute-break-debug. It's edit-debug.

Upvotes: 10

Jerry Coffin
Jerry Coffin

Reputation: 490108

Boost is C++, not C (emphatically not C -- virtually all of it makes heavy use of templates and such that aren't part of C).

Yes, C programmers tend to build up personal libraries of code for all sorts of "stuff" -- data structures, algorithms, user interfaces, and so on. There are also a fair number of other libraries for everything from basic string manipulation to database connectivity, user interfaces, basic algorithms and data structures, etc.

Comparing productivity between the two can be difficult though -- even if something can be done in one line of code either way, there's a greater chance that the C programmer will end up doing extra work to find and learn to use that particular library. OTOH, if he has used it before, the two might be directly competitive of (in a few cases) C might be more productive.

I'd guess Python ends up more productive more often, but trying to guess how much so is difficult (and lines of code usually won't be a good indication either).

Upvotes: 6

stacker
stacker

Reputation: 68962

  1. As I did serious c programming I read a book that claimed libraries are worth to write. (Especially in C which considered a low level language)

  2. Libraries are build for reuse.

  3. If you use libraries you write one line like detectFace( faceDesriptor ) or renderPDF( document) is doesn't matter whether an idiom in another language is more concise or not. Lines of code isn't a proper metric if it is about what would more efficient.

Upvotes: 1

Related Questions