SimplifyC++ Article
Comparing the Speed of Counting from 1 to Million C++ vs Rust
Comparing the Speed of Counting from 1 to Million: C++ vs Rust
In this article, we will design a simple program to count from 1 to 1 million using both C++ and Rust, then measure the time each program takes to complete. The goal is to compare the performance of these two popular programming languages in a straightforward computational task. The results will provide insights into their execution speed in similar conditions.
C++ Code
int main() { auto start = std::chrono::high_resolution_clock::now();
for (int i = 1; i <= 1000000; ++i) { // Counting only, no printing to avoid performance overhead }
auto end = std::chrono::high_resolution_clock::now(); std::chrono::duration<double> elapsed = end - start; std::cout << "Time taken by C++: " << elapsed.count() << " seconds" << std::endl;
return 0;}How to Run the C++ Code
Save the code in a file named
counter.cpp.Compile the code using a C++ compiler like g++ :
g++ -O2 -o counter counter.cppThe
-O2flag enables optimization for better performance.
Execute the compiled program:
./counter
Rust Code
use std::time::Instant;
fn main() { let start = Instant::now();
for _ in 1..=1_000_000 { // Counting only, no printing }
let duration = start.elapsed(); println!("Time taken by Rust: {:.6} seconds", duration.as_secs_f64());}How to Run the Rust Code
Save the code in a file named
counter.rs.Compile the code using the Rust compiler (rustc):
rustc -C opt-level=2 counter.rsThe
-C opt-level=2flag enables performance optimization.
Run the compiled program:
./counter
Performance Analysis
Factors Influencing Performance:
Hardware:
CPU and memory specifications affect execution time.
Compiler Optimizations:
The level of optimization (
-O2for C++ or-C opt-level=2for Rust) impacts the speed significantly.
Operating System:
Variations in system-level libraries and runtime behavior can influence results.
Observations on Results:
Both C++ and Rust are highly optimized for tasks like counting, so their performance is extremely close.
Rust includes built-in features such as integer overflow protection, which might slightly affect execution time. However, this is disabled when optimizations are enabled.
With proper optimization flags, C++ and Rust achieve nearly identical performance.
Sample Results (Approximate)
Using the same environment (Intel Core i7, Linux OS), the following results were observed:
C++: 0.0022 seconds.
Rust: 0.0025 seconds.
These differences are negligible and may vary depending on the environment and compiler versions.
Conclusion
Performance: C++ and Rust are almost equally fast for computationally simple tasks like counting, with differences being minimal when optimizations are applied.
Optimization Importance: The use of compiler optimization flags (
-O2for C++ and-C opt-level=2for Rust) is crucial for achieving maximum performance.Practical Consideration: If execution time is critical, ensure proper benchmarking and tuning of your code. For most real-world applications, the performance gap between these two languages is insignificant.
Ultimately, choosing between C++ and Rust depends more on other factors such as ecosystem, safety features, and project requirements rather than raw execution speed.
What did you think?
Sign in to react or comment.
Comments
0No comments yet.