Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is C inefficient compared to Assembly? [duplicate]

This is purely a theory question, so, given an "infinite" time to make a trivial program, and an advanced knowledge of C and Assembly, is it really better to do something in Assembly? is "performance" lost when compiling C into Assembly (to machine code)?

By performance I mean, do modern C compilers do a bad job at certain tasks that programming directly in Assembly speeds up?

like image 912
Manux Avatar asked Jul 22 '10 23:07

Manux


2 Answers

Modern C can do a better job than assembly in many cases, because keeping track of which operations can overlap and which will block others is so complex it can only be reasonably be tracked by a computer.

like image 63
Mark Ransom Avatar answered Sep 20 '22 17:09

Mark Ransom


C is not inefficient compared to anything. C is a language, and we don't describe languages in terms of efficiency. We compare programs in terms of efficiency. C doesn't write programs; programmers write programs.

Assembly gives you immense flexibility when comparing with C, and that is at the cost of time programming. If you are a guru C programmer and a guru Assembly programmer, then chances are you might be able to squeeze some more juice with Assembly for writing any given program, but the price for that is virtually certain to be prohibitive.

Most of us aren't gurus in either of these languages. For most of us, giving the responsibility of performance tuning to a C compiler is a double win: you get the wisdom of a number of Assembly gurus, the people who wrote the C compiler, along with an immense amount of time in your hands to further correct and enhance your C program. You also get portability as a bonus.

like image 22
wilhelmtell Avatar answered Sep 19 '22 17:09

wilhelmtell