binarytrees_jit.lua 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. -- Binary trees benchmark from benchmarks game
  2. -- https://benchmarksgame-team.pages.debian.net/benchmarksgame/description/binarytrees.html
  3. --
  4. -- This is a small GC-focused benchmark, with not much room for clever tricks.
  5. -- The code used here is based on the one contributed by Mike Pall:
  6. -- * The leaf nodes are now store `false` instead of being empty tables.
  7. -- This is more consistent with what is done in other languages.
  8. -- * Use print instead of io.write
  9. -- * Use bitshift operator in Lua 5.4 and Pallene
  10. --
  11. -- Expected output (N = 21)
  12. -- stretch tree of depth 22 check: 8388607
  13. -- 2097152 trees of depth 4 check: 65011712
  14. -- 524288 trees of depth 6 check: 66584576
  15. -- 131072 trees of depth 8 check: 66977792
  16. -- 32768 trees of depth 10 check: 67076096
  17. -- 8192 trees of depth 12 check: 67100672
  18. -- 2048 trees of depth 14 check: 67106816
  19. -- 512 trees of depth 16 check: 67108352
  20. -- 128 trees of depth 18 check: 67108736
  21. -- 32 trees of depth 20 check: 67108832
  22. -- long lived tree of depth 21 check: 4194303
  23. local function BottomUpTree(depth)
  24. if depth > 0 then
  25. depth = depth - 1
  26. local left = BottomUpTree(depth)
  27. local right = BottomUpTree(depth)
  28. return { left, right }
  29. else
  30. return { false, false }
  31. end
  32. end
  33. local function ItemCheck(tree)
  34. if tree[1] then
  35. return 1 + ItemCheck(tree[1]) + ItemCheck(tree[2])
  36. else
  37. return 1
  38. end
  39. end
  40. local function Stress(mindepth, maxdepth, depth)
  41. local iterations = 2 ^ (maxdepth - depth + mindepth)
  42. local check = 0
  43. for _ = 1, iterations do
  44. local t = BottomUpTree(depth)
  45. check = check + ItemCheck(t)
  46. end
  47. return { iterations, check }
  48. end
  49. return function(N)
  50. N = N or 10
  51. local mindepth = 4
  52. local maxdepth = math.max(mindepth + 2, N)
  53. do
  54. local stretchdepth = maxdepth + 1
  55. local stretchtree = BottomUpTree(stretchdepth)
  56. print(string.format("stretch tree of depth %d\t check: %d",
  57. stretchdepth, ItemCheck(stretchtree)))
  58. end
  59. local longlivedtree = BottomUpTree(maxdepth)
  60. for depth = mindepth, maxdepth, 2 do
  61. local r = Stress(mindepth, maxdepth, depth)
  62. local iterations = r[1]
  63. local check = r[2]
  64. print(string.format("%d\t trees of depth %d\t check: %d",
  65. iterations, depth, check))
  66. end
  67. print(string.format("long lived tree of depth %d\t check: %d",
  68. maxdepth, ItemCheck(longlivedtree)))
  69. end