Live data from Hacker News

How many lines of C it takes to execute a + b in Python

codeconfessions.substack.com

21–30 of 224 posts

Re: How many lines of C it takes to execute a + b in Python

#22
post #17
post #5

Sounds like it would be easier to just use C anyway.

Yes, but C is a much more arcane and complicated language. Case in point: instead of writing a+b in Python, in C you would have to write a+b (Jokes aside, it would really be more complicated in C if a and b were actually strings or lists.)

Or numbers that add up to over the size limit of what ever byte/int/long type you are using.

There's a surprising amount of depth to adding two numbers.

Re: How many lines of C it takes to execute a + b in Python

#23
post #17
post #5

Sounds like it would be easier to just use C anyway.

Yes, but C is a much more arcane and complicated language. Case in point: instead of writing a+b in Python, in C you would have to write a+b (Jokes aside, it would really be more complicated in C if a and b were actually strings or lists.)

If a and b are strings, Python would make "ab".

Re: How many lines of C it takes to execute a + b in Python

#26
post #17
post #5

Sounds like it would be easier to just use C anyway.

Yes, but C is a much more arcane and complicated language. Case in point: instead of writing a+b in Python, in C you would have to write a+b (Jokes aside, it would really be more complicated in C if a and b were actually strings or lists.)

> it would really be more complicated in C if a and b were actually strings or lists

You don't event need strings or lists for that. Just imagine bigger numbers for a and b. Arbitrarily long integer addition is not a native language feature in C.

edit: formatting

Re: How many lines of C it takes to execute a + b in Python

#28
post #20

Lines of C aren't the defining factor. You want total instructions executed.

IME I'd say a line of typical C code (not necessarily C++ code) maps to around 1..5 instructions on average. It's still a quite useful measure to get a rough idea how much CPU work happens.

Re: How many lines of C it takes to execute a + b in Python

#29
Python's math operations are going to need a lot of C code because the numbers can be any size. It's part of what makes it so great for scientific computing (as you don't have to spend hours implementing arbitrary precision math - probably badly.) If that's too slow though there's always NumPy.

Re: How many lines of C it takes to execute a + b in Python

#30

Python's math operations are going to need a lot of C code because the numbers can be any size. It's part of what makes it so great for scientific computing (as you don't have to spend hours implementing arbitrary precision math - probably badly.) If that's too slow though there's always NumPy.

That's only true for integers.

For scientific applications you'd typically want floating points and Python's floats are just regular ieee-754 doubles (or whatever "double" meant to the compiler used to compile that python interpreter).

Post reply on HN