Comparative speed of elementary operations in Ruby and Jruby. Loop comparison

    Good day.
    image
    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
    1. ################################################
    2. x = 1_000_000
    3. a = Time.new
    4. 1_000_000.times do
    5.     x -= 3
    6. end
    7. b = Time.new
    8. puts b - a
    9. ################################################
    10. x = 1
    11. a = Time.new
    12. for i in 1..1_000_000 do
    13.     x += 3
    14. end
    15. b = Time.new
    16. puts b - a
    17. ################################################
    18. x = 1
    19. i =  0
    20. a = Time.new
    21. while i < 1_000_000 do
    22.     x += 3
    23.     i += 1
    24. end
    25. b = Time.new
    26. puts b - a
    27. ################################################
    28. x = 1
    29. a = Time.new
    30. 10_000.times do
    31.     x *= 3
    32. end
    33. b = Time.new
    34. puts b - a
    35. ################################################
    36. x = 1
    37. a = Time.new
    38. for i in 1..10_000 do
    39.     x *= 3
    40. end
    41. b = Time.new
    42. puts b - a
    43. ################################################
    44. x = 1
    45. i =  0
    46. a = Time.new
    47. while i < 10_000 do
    48.     x *= 3
    49.     i += 1
    50. end
    51. b = Time.new
    52. puts b - a
    53. ################################################
    54. x = 3**10_000
    55. a = Time.new
    56. 10_000.times do
    57.     x /= 3
    58. end
    59. b = Time.new
    60. puts b - a
    61. ################################################
    62. x = 3**10_000
    63. a = Time.new
    64. for i in 1..10_000 do
    65.     x /= 3
    66. end
    67. b = Time.new
    68. puts b - a
    69. ################################################
    70. x = 3**10_000
    71. i =  0
    72. a = Time.new
    73. while i < 10_000 do
    74.     x /= 3
    75.     i += 1
    76. end
    77. b = Time.new
    78. puts b - a
    79. ################################################ 


    Results:

    image

    image

    image

    The “times” cycle is more advantageous to use when adding.
    image

    The while loop is better for multiplication.
    image

    When dividing for ruby ​​- “times”, for jruby - “while”
    image

    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."
    image

    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

    Also popular now: