This simple benchmark compares the speed at which different languages are able
to sum values:
∑
i
=
0
10000
i
Unsurprisingly, the Rust implementation was the fastest. This is likely due to
Rust being a compiled language while the other languages are interpreted. This
shows that Bash is not designed to perform arithmetic operations since it is
slow at addition and exceptionally slow at recursion .
Instead, Bash should be used for calling external commands since it is the
fastest command interpreter .
Execution times for calculating the series sum of 100,000:
Language
Mean [ms]
Min [ms]
Max [ms]
Relative
rustc 1.79.0
24.4 ± 11.2
7.7
39.4
1.00
perl 5.38.2
28.1 ± 11.5
9.3
43.5
1.15 ± 0.71
python 3.11.9
35.1 ± 12.6
16.5
48.1
1.44 ± 0.84
node 22.2.0
46.7 ± 10.5
33.1
63.2
1.91 ± 0.98
bash 5.2.26
307.2 ± 13.6
289.6
325.4
12.59 ± 5.81
Implementations
Rust
# !/ usr / bin / env cached - nix - shell
/*
#!nix-shell -i rust-script -p rustc -p rust-script -p cargo
*/
//! Do arithmetic operations.
use std :: env :: args ;
/// Perform sum operations.
///
/// * `operations`: The number of operations to perform.
///
/// Returns the series sum.
fn do_sum ( operations : i32 ) -> i32 {
let mut counter = 0 ;
for i in 0 .. operations + 1 {
counter = counter + i ;
}
return counter ;
}
/// Do the number of arithmetic operations specified by the user input.
fn main () {
let arguments : Vec < String > = args () .collect ();
if let Some ( argument ) = arguments .get ( 1 ) {
let operations : i32 = argument .parse () .unwrap_or ( 0 );
let result = do_sum ( operations );
println! ( "{}" , result );
}
}
Perl
#!/usr/bin/env perl
use strict ;
use warnings ;
=head1 DESCRIPTION
Do arithmetic operations.
=head1 FUNCTIONS
=over
=item count($total)
Perform sum operations.
$total C<int> is the number of operations to perform.
Returns C<int>, the series sum.
=back
=cut
sub doSum {
my $total = shift ;
my $sum = 0 ;
for my $i ( 1 .. $total ) {
$sum = $sum + $i ;
}
return $sum ;
}
print doSum shift ;
Python
#!/usr/bin/env python3
"""Do arithmetic operations."""
from sys import argv
def do_sum ( operations : int ) -> int :
"""Perform sum operations.
Args:
operations (int): The number of operations to perform.
Returns:
int: The series sum.
"""
counter = 0
for i in range ( operations + 1 ):
counter = counter + i
return counter
def main ():
"""Calculate a Fibonacci sequence number from user input."""
term = int ( argv [ 1 ])
print ( do_sum ( term ))
if __name__ == "__main__" :
main ()
JavaScript
#!/usr/bin/env node
/**
* @module Do arithmetic operations.
*/
/**
* Perform sum operations.
*
* @param {number} operations The number of operations to perform.
* @returns {number} The series sum.
*/
function doSum ( operations ) {
let sum = 0 ;
for ( let i = 0 ; i <= operations ; i ++ ) {
sum = sum + i ;
}
return sum ;
}
const term = parseInt ( process . argv [ 2 ]);
console . log ( doSum ( term ));
Bash
#!/usr/bin/env bash
# Do arithmetic operations.
#######################################
# Perform sum operations.
# Arguments:
# $1: The number of operations to perform
# Returns:
# int: The series sum.
#######################################
count() {
local sum = 0
for (( i = 0; i <= $1 ; i++)) ; do
(( sum = sum + i))
done
echo $sum
}
count " $1 "