Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does C code run faster?

Is there any performance gain in calling C code from Objective-C?

I've read somewhere that message passing is slower compared to other languages that use function calling. So if I call a C function from Objective-C code, am I avoiding the messaging overhead?

When optimizing for performance, is it recommended practice to code the most critical functions and procedures in C instead of using Objective-C objects?

EDIT:
Given the amount of answers warning about premature optimization and code readability, I want to clarify that I was not thinking on regular applications, but very specific ones such as:

  • Graphics
  • Encryption or compression algorithms.
  • Maths

And in general, functions or procedures that do not need OO design and are intended to be called many times with parameters.

like image 978
Mister Smith Avatar asked Mar 07 '12 16:03

Mister Smith


People also ask

Does C run faster than C++?

C is faster than C++ C++ allows you to write abstractions that compile-down to equivalent C. This means that with some care, a C++ program will be at least as fast as a C one. The advantage C++ gives over C is that it enables us to also build reusable abstractions with templates, OOP and functional composition.

Does C run faster than Python?

C is a faster language compared to Python as it is compiled. Python programs are usually slower than C programs as they are interpreted. In C, the type of the various variables must be declared when they are created, and only values of those particular types must be assigned to them.

Why C program is fast?

The programs that you write in C compile and execute much faster than those written in other languages. This is because it does not have garbage collection and other such additional processing overheads. Hence, the language is faster as compared to most other programming languages.

Is C code faster than Java?

Java is compiled into a lower language, then interpreted. It also has automatic garbage collection, and it's farther from machine code in the first place. Because of this C code tends to run faster than Java, but difference depends on what's being done and how well the code has been optimized.


1 Answers

It's highly unlikely that you're writing code that's optimized enough for the bottleneck to be message sends. Write your code in the way that makes the most sense to you, then use Instruments to profile and optimize if necessary. Almost certainly, if your code is slow at all, it will be due to much higher-level problems than message sending.

like image 172
andyvn22 Avatar answered Oct 09 '22 12:10

andyvn22