Comparative speed of elementary operations in Ruby and Jruby. Loop comparison
Good day.
If someone is interested in learning about the speed of performing elementary operations in Ruby and Jruby, as well as the speed of the "for", "while", "times" loops, then you
don’t know what Ruby, Jruby is.
Information from ru.wikipedia.org
Ruby is a dynamic, reflective, high-level interpreted language for fast and convenient object-oriented programming. ( Ru.wikipedia.org/wiki/Ruby )
JRuby is a ruby programming language interpreter written entirely in Java (original the interpreter is written in C). ( en.wikipedia.org/wiki/JRuby )
By “elementary” operations I understand: “addition”, “subtraction” (although I did not include it in the review), “multiplication”, “division”. I can’t say that I did everything right (probably there is something that I don’t know about), I would like to hear about mistakes. It is important to note that it is impossible to compare "addition", "multiplication", "division" relative to each other, since there are different numbers of iterations.
The test was as follows:
1) There are 9 script files: [3 cycles] x [3 elementary operations]. In other words: “addition” in the “for” loop, “addition” in the “while” loop, “addition” in the “times” loop, “multiplication” in the “for” loop, and so on.
2) Each file is executed 10 times - 5 times for ruby and 5 times for jruby.
3) The results are recorded in the table.
4) Conclusions are made.
Computer configuration:
Notebook Asus eeepc 900
Processor: Intel Celeron 353 ULV at a frequency of 900MHz
RAM: 1Gb RAM DDR2 PC3200
Debian Squeeze
ruby 1.8.7
jruby 1.4.0
Code (###### - file separator):
Results:
The “times” cycle is more advantageous to use when adding.
The while loop is better for multiplication.
When dividing for ruby - “times”, for jruby - “while”
As a result, it cannot be said that JRuby or Ruby is “faster”, for subtraction the results are approximately equivalent to addition. Perhaps this diagram is misleading - “addition”, “multiplication”, “division” on the same graph, but the number of repetitions in cycles is different, you need to look at “addition” Ruby vs “addition” JRuby, “multiplication” Ruby vs “multiplication” JRuby and the same with "division."
We must not forget that “Java classes can be called from Ruby code in JRuby, so you can access all the libraries, infrastructures and tools of the Java platform. You can also access Ruby code from Java. There is support for most of the built-in classes, BSF [1].
The standard use of JRuby is to embed it in a Java application to support scripting and speed up development, which is an advantage of Ruby over static languages. It can also be used to run Rails applications on Java platforms. ” - en.wikipedia.org/wiki/JRuby
If someone is interested in learning about the speed of performing elementary operations in Ruby and Jruby, as well as the speed of the "for", "while", "times" loops, then you
don’t know what Ruby, Jruby is.
Information from ru.wikipedia.org
Ruby is a dynamic, reflective, high-level interpreted language for fast and convenient object-oriented programming. ( Ru.wikipedia.org/wiki/Ruby )
JRuby is a ruby programming language interpreter written entirely in Java (original the interpreter is written in C). ( en.wikipedia.org/wiki/JRuby )
By “elementary” operations I understand: “addition”, “subtraction” (although I did not include it in the review), “multiplication”, “division”. I can’t say that I did everything right (probably there is something that I don’t know about), I would like to hear about mistakes. It is important to note that it is impossible to compare "addition", "multiplication", "division" relative to each other, since there are different numbers of iterations.
The test was as follows:
1) There are 9 script files: [3 cycles] x [3 elementary operations]. In other words: “addition” in the “for” loop, “addition” in the “while” loop, “addition” in the “times” loop, “multiplication” in the “for” loop, and so on.
2) Each file is executed 10 times - 5 times for ruby and 5 times for jruby.
3) The results are recorded in the table.
4) Conclusions are made.
Computer configuration:
Notebook Asus eeepc 900
Processor: Intel Celeron 353 ULV at a frequency of 900MHz
RAM: 1Gb RAM DDR2 PC3200
Debian Squeeze
ruby 1.8.7
jruby 1.4.0
Code (###### - file separator):
Copy Source | Copy HTML- ################################################
- x = 1_000_000
- a = Time.new
- 1_000_000.times do
- x -= 3
- end
- b = Time.new
- puts b - a
- ################################################
- x = 1
- a = Time.new
- for i in 1..1_000_000 do
- x += 3
- end
- b = Time.new
- puts b - a
- ################################################
- x = 1
- i = 0
- a = Time.new
- while i < 1_000_000 do
- x += 3
- i += 1
- end
- b = Time.new
- puts b - a
- ################################################
- x = 1
- a = Time.new
- 10_000.times do
- x *= 3
- end
- b = Time.new
- puts b - a
- ################################################
- x = 1
- a = Time.new
- for i in 1..10_000 do
- x *= 3
- end
- b = Time.new
- puts b - a
- ################################################
- x = 1
- i = 0
- a = Time.new
- while i < 10_000 do
- x *= 3
- i += 1
- end
- b = Time.new
- puts b - a
- ################################################
- x = 3**10_000
- a = Time.new
- 10_000.times do
- x /= 3
- end
- b = Time.new
- puts b - a
- ################################################
- x = 3**10_000
- a = Time.new
- for i in 1..10_000 do
- x /= 3
- end
- b = Time.new
- puts b - a
- ################################################
- x = 3**10_000
- i = 0
- a = Time.new
- while i < 10_000 do
- x /= 3
- i += 1
- end
- b = Time.new
- puts b - a
- ################################################
Results:
The “times” cycle is more advantageous to use when adding.
The while loop is better for multiplication.
When dividing for ruby - “times”, for jruby - “while”
As a result, it cannot be said that JRuby or Ruby is “faster”, for subtraction the results are approximately equivalent to addition. Perhaps this diagram is misleading - “addition”, “multiplication”, “division” on the same graph, but the number of repetitions in cycles is different, you need to look at “addition” Ruby vs “addition” JRuby, “multiplication” Ruby vs “multiplication” JRuby and the same with "division."
We must not forget that “Java classes can be called from Ruby code in JRuby, so you can access all the libraries, infrastructures and tools of the Java platform. You can also access Ruby code from Java. There is support for most of the built-in classes, BSF [1].
The standard use of JRuby is to embed it in a Java application to support scripting and speed up development, which is an advantage of Ruby over static languages. It can also be used to run Rails applications on Java platforms. ” - en.wikipedia.org/wiki/JRuby