Tutorialspoint.dev

Why is C considered faster than other languages ?


You might have came across these statements, C is more optimised or performance of C is better than higher languages, so I’ll be discussing the reasons for this hypothesis.

First let’s list out functionalities which are provided by languages like Java and not C :

  1. Array index bound checking
  2. Uninitialized variable values checking
  3. Check for memory leaks
  4. Check for null pointer dereference
  5. Automatic garbage collection
  6. Run-time type checking
  7. Exception handling

and there are more such features which are not present in C.

Extra features comes at cost and the cost includes decreased speed and increased size.

Let’s take an example for dynamic allocation in C and Java
Java



MyClass obj = new MyClass();

Did you considered size of obj, the answer is No. The reason being it is automatically handled by language itself in background and you don’t have to write specific code for it.

But in case of C

struct MyStruct *obj = malloc(sizeof(struct MyStruct));

As you can see in above code the tasks of assigning reference to pointer, allocation of size is done explictly by programmer and at last free up allocated memory.

The array bound check is supported by Thumb Execution Environment (ThumbEE), its other features includes automatic null pointer checks on every load and store instruction, an special instruction that call a handler.

Another reason is closeness of C to the Assembly language, in most of the cases its instructions directly map to assembly language, C is only one or level two levels of abstraction away from assembly language while Java is at minimum 3 levels abstraction away from assembler.

References :
1) why-is-c-so-fast-and-why-arent-other-languages-as-fast-or-faster
2) ARM_architecture#Thumb_Execution_Environment_.28ThumbEE.29
3) Linus Torvalds view

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



This article is attributed to GeeksforGeeks.org

tags:

C C

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter