PageRenderTime 23ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/bench/truffle/metrics/mandelbrot.rb

http://github.com/jruby/jruby
Ruby | 110 lines | 54 code | 13 blank | 43 comment | 7 complexity | fa12fe2bfd0120bc35301fb11dd4db8d MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause, GPL-2.0, JSON, LGPL-2.1
  1. # Copyright © 2004-2013 Brent Fulgham
  2. #
  3. # All rights reserved.
  4. #
  5. # Redistribution and use in source and binary forms, with or without
  6. # modification, are permitted provided that the following conditions are met:
  7. #
  8. # * Redistributions of source code must retain the above copyright notice,
  9. # this list of conditions and the following disclaimer.
  10. #
  11. # * Redistributions in binary form must reproduce the above copyright notice,
  12. # this list of conditions and the following disclaimer in the documentation
  13. # and/or other materials provided with the distribution.
  14. #
  15. # * Neither the name of "The Computer Language Benchmarks Game" nor the name
  16. # of "The Computer Language Shootout Benchmarks" nor the names of its
  17. # contributors may be used to endorse or promote products derived from this
  18. # software without specific prior written permission.
  19. #
  20. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23. # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  24. # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  26. # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  27. # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. # The Computer Language Benchmarks Game
  31. # http://benchmarksgame.alioth.debian.org
  32. #
  33. # contributed by Karl von Laudermann
  34. # modified by Jeremy Echols
  35. # modified by Detlef Reichl
  36. # modified by Joseph LaFata
  37. # modified by Peter Zotov
  38. # http://benchmarksgame.alioth.debian.org/u64q/program.php?test=mandelbrot&lang=yarv&id=3
  39. def mandelbrot(size)
  40. Truffle::Graal.assert_not_compiled
  41. sum = 0
  42. byte_acc = 0
  43. bit_num = 0
  44. y = 0
  45. while y < size
  46. ci = (2.0*y/size)-1.0
  47. x = 0
  48. while x < size
  49. zrzr = zr = 0.0
  50. zizi = zi = 0.0
  51. cr = (2.0*x/size)-1.5
  52. escape = 0b1
  53. z = 0
  54. while z < 50
  55. tr = zrzr - zizi + cr
  56. ti = 2.0*zr*zi + ci
  57. zr = tr
  58. zi = ti
  59. # preserve recalculation
  60. zrzr = zr*zr
  61. zizi = zi*zi
  62. if zrzr+zizi > 4.0
  63. escape = 0b0
  64. break
  65. end
  66. z += 1
  67. end
  68. byte_acc = (byte_acc << 1) | escape
  69. bit_num += 1
  70. # Code is very similar for these cases, but using separate blocks
  71. # ensures we skip the shifting when it's unnecessary, which is most cases.
  72. if (bit_num == 8)
  73. #print byte_acc.chr
  74. sum ^= byte_acc
  75. byte_acc = 0
  76. bit_num = 0
  77. elsif (x == size - 1)
  78. byte_acc <<= (8 - bit_num)
  79. #print byte_acc.chr
  80. sum ^= byte_acc
  81. byte_acc = 0
  82. bit_num = 0
  83. end
  84. x += 1
  85. end
  86. y += 1
  87. end
  88. sum
  89. end
  90. start = Time.now
  91. begin
  92. loop do
  93. mandelbrot 750
  94. end
  95. rescue RubyTruffleError
  96. puts Time.now - start
  97. end