Table of Contents
When it comes to programming, one of the most debated topics is execution speed. C vs Python speed comparison is crucial for developers choosing between these languages. In this article, we will compare their execution speed using a simple example.
Understanding the Speed Difference
C is a compiled language, meaning the code is translated into machine code before execution. This results in faster performance because the CPU executes instructions directly without any intermediary.
Python, on the other hand, is an interpreted language. It is translated into bytecode, which is then executed by the Python Virtual Machine (PVM). This added layer of abstraction makes Python slower in comparison to C.
C vs Python Speed Comparison: Fibonacci Sequence Computation
Let’s take two examples to compare the differences.
Example 1: Fibonacci Sequence Computation
In this example, we will compute the nth Fibonacci number using a recursive approach in both C and Python
C Code:
#include <stdio.h> #include <time.h> long long fibonacci(int n) { if (n <= 1) return n; return fibonacci(n - 1) + fibonacci(n - 2); } int main() { int n = 40; clock_t start = clock(); long long result = fibonacci(n); clock_t end = clock(); double time_taken = (double)(end - start) / CLOCKS_PER_SEC; printf("Fibonacci(%d) = %lld\n", n, result); printf("Time taken in C: %f seconds\n", time_taken); return 0; }
Output:
The output of this code is
Fibonacci(40) = 102334155
Time taken in C: 0.651000 seconds
Python Code:
import time def fibonacci(n): if n <= 1: return n return fibonacci(n - 1) + fibonacci(n - 2) n = 40 start = time.time() result = fibonacci(n) end = time.time() print(f"Fibonacci({n}) = {result}") print(f"Time taken in Python: {end - start} seconds")
Output:
The output of this code is:
Fibonacci(40) = 102334155 Time taken in Python: 26.663263082504272 seconds
Execution Time Comparison
When running the above code for n = 40
:
- C executes in 0.651 seconds
- Python takes 26.663 seconds
Example 2: Running Large Loops
In this example, we will just run multiple loops. We will run the same number of loops and perform the same operations inside the loops, a simple addition.
C Code:
#include <stdio.h> #include <time.h> int main() { clock_t start, end; double cpu_time_used; int a = 0; int i = 0; start = clock(); for(i=0;i<10000000;i++) { a = a+1; } for(i=0;i<10000000;i++) { a = a+1; } for(i=0;i<10000000;i++) { a = a+1; } end = clock(); cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC; printf("Time taken: %f seconds\n", cpu_time_used); printf("%d",a); return 0; }
Output:
The output of this code is
Time taken: 0.020000 seconds
30000000
Python Code:
# Import time module import time # record start time start = time.time() # define a sample code segment a = 0 for i in range(10000000): a=a+1 for i in range(10000000): a=a+1 for i in range(10000000): a=a+1 # record end time end = time.time() # print the difference between start # and end time in milli. secs print("The time of execution of above program is :", (end-start) * 10**3, "ms") print(a)
Output:
The output of this code is
The time of execution of above program is : 4319.210767745972 ms
30000000
Execution Time Comparison
When running the above code for n = 40
:
- C executes in 0.651 seconds
- Python takes 26.663 seconds
Another benchmark using large loops:
- C executes in 0.020 seconds (20 milliseconds)
- Python takes 4319.210 milliseconds (4.319 seconds)
How Much Faster is C?
Based on these tests:
- For Fibonacci computation, C is approximately 40 times faster than Python.
- For large loops, C is over 200 times faster than Python.
Why is C Faster?
- Compiled vs Interpreted: C code is directly executed as machine code, whereas Python has an interpretation overhead.
- Memory Management: C provides manual memory management, reducing unnecessary overhead.
- Function Call Overhead: Python’s function calls are more expensive due to dynamic typing and recursion depth checks.
Optimizing Python
If we use memoization (caching results of previous function calls) in Python using lru_cache
, we can dramatically speed up execution:
from functools import lru_cache import time @lru_cache(None) def fibonacci(n): if n <= 1: return n return fibonacci(n - 1) + fibonacci(n - 2) n = 40 start = time.time() result = fibonacci(n) end = time.time() print(f"Fibonacci({n}) = {result}") print(f"Time taken in Python (optimized): {end - start} seconds")
With memoization, the execution time is reduced drastically and completes almost instantly because the cache stores previously computed values, eliminating redundant recursive calls.
Internal and External Resources
For more details on Python performance optimizations, check out Python’s official performance guide.
If you’re interested in learning more about C programming, visit our guide on Getting Started with C.
You can also follow the full beginner course on Youtube:
Conclusion
This C vs Python speed comparison shows that C is much faster than Python for CPU-intensive tasks due to its compiled nature and efficient memory handling. However, Python offers ease of use, flexibility, and optimizations like caching to improve performance.
For computationally heavy tasks, consider using C for performance-critical sections or leveraging libraries like NumPy in Python, which use C under the hood.
Which language do you prefer for speed-critical applications? Let us know in the comments!
Hi, I’m Vivek, a Senior Embedded Innovation Specialist. I have been working on Embedded Systems and IoT for the past 11 years. I love to share my knowledge and train those who are interested. Nerdyelectronics.com was started out of this interest. You can read my full profile in this link.