Article by Ayman Alheraki on January 11 2026 10:35 AM
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.
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;}Save the code in a file named counter.cpp.
Compile the code using a C++ compiler like g++ :
g++ -O2 -o counter counter.cppThe -O2 flag enables optimization for better performance.
Execute the compiled program:
./counteruse 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());}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=2 flag enables performance optimization.
Run the compiled program:
./counterHardware:
CPU and memory specifications affect execution time.
Compiler Optimizations:
The level of optimization (-O2 for C++ or -C opt-level=2 for Rust) impacts the speed significantly.
Operating System:
Variations in system-level libraries and runtime behavior can influence 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.
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.
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 (-O2 for C++ and -C opt-level=2 for 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.