Comparative study of looping construct in ruby, dlang, rust, golang.

We have been learning & working on various programmings languages. These different programmings language help us to learn various programming paradigm & constructs.  There are number of new programming language such golang, rust, dlang.

We are going to see performance of simple loop in golang, rust, dlang, ruby. We are going iterate a 100 million times to see raw performance. Below is the example of for loop in golang.

package main

func main(){
  for i := 0; i < 100000000; i++ {
   fmt.Println("i am new task ", i);
  } 
}

Above code is compiled with gccgo-4.9(golang). gccgo compile the given input file & produces binary file with name ‘a.out’.Benchmark of following code:

#golang performance
real 0m0.308s
user 0m0.305s
sys  0m0.004s

Below is the example of loop in rust-lang. In rust lang, there are two different looping construct namely ‘for’ and
‘while’ loop. ‘while’ denotes a loop that iterates as long as its given condition.

fn main(){
  let mut x:int = 0;
  while x < 100000000 {
   x += 1;
  }

}

Above code is compiled with rust(version 0.13).  rustc compile the given input file & produce binary file with name same name as input.Benchmark of following code:

real 0m0.336s
user 0m0.325s
sys  0m0.011s

Below looping construct is of ‘dlang’ language.

import std.stdio;
void main(){
  ulong i = 0;
  while(i < 100000000){
   i = i + 1;
  }

}

Above code is compiled with dlang(version 0.13).  ‘dmd’ compile the given input file & produce binary file with name same name as input.Benchmark of following code:

Below code is  while loop construct from ‘ruby’ language.

i=0
while(i<100000000) do
 i = i + 1
end

Above code is compiled with ruby 2.1.2. Benchmark of following code is shown as below:

real 0m2.427s
user 0m2.423s
sys  0m0.006s

Below code is  while loop construct from ‘ruby’ language.

for i in (0..100000000)

end

Above code is compiled with ruby 2.1.2. Benchmark of following code is shown as below:

real 0m6.070s
user 0m5.716s
sys  0m0.014s

We have seen that compiled language performed much better in simple construct such loop than interpreted language. Golang/rust/c language compilation time is as nearer as C language.

In ruby, ‘while’ loop performs much better than ‘for’. It is recommended that when iterating large data,use ‘while’ or ‘loop’ than the traditional each or foreach.

Rust lang still is in heavy development phase & will be expected to attain stability soon later these year.
Golang performance is much better but it is still lagging behind C language.
Rust is expected to be a good replacement for low level language. Even some kernal specific code can be written with rust as it contain pointer in it. Golang is seen as more like of general purpose language with garbage collection support just like higher level langauage. All three language golang, rust, dlang have support for concurreny.

Rust seem to be promising for low level task whereas golang seem to striving as general purpose.


One thought on “Comparative study of looping construct in ruby, dlang, rust, golang.

  1. Reblogged this on Josh Software – Where Programming is an Art! and commented:

    Sanjiv has drawn a good raw loop performance comparison between Go, Rust, C and Dlang. Some of the outcomes are pretty fascinating and he predicts that Rust will be a great for low level and system programming while Go will take the lead for general purpose high level languages.

    Did you know – In ruby, ‘while’ loop performs much better than ‘for’. It is recommended that when iterating large data,use ‘while’ or ‘loop’ than the traditional each or foreach.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s