Guest User

ComputerCraft Speed Test Program

a guest
Apr 28th, 2014
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.12 KB | None | 0 0
  1. local advanced = term.isColor()
  2. local function setTextColor(colornum)
  3.   if advanced then
  4.     term.setTextColor(colornum)
  5.   end
  6. end
  7.  
  8. term.clear()
  9. term.setCursorPos(1,1)
  10. setTextColor(colors.yellow)
  11. print("Computer Speed Test")
  12. setTextColor(colors.white)
  13. print("Hertz Meassurements")
  14. print()
  15. print("How many +1 Calculations done!")
  16. term.setCursorPos(1,6)
  17. local function result(name, value)
  18.   setTextColor(colors.yellow)
  19.   term.write(name..": ")
  20.   setTextColor(colors.white)
  21.   print(value)
  22. end
  23.  
  24. local cycleTable = {}
  25. local cycleCount = 20 --Musn't be changed!
  26.  
  27. for i = 1,cycleCount do
  28.   clock = os.clock()
  29.   stop = clock + (1 / cycleCount)
  30.   local cycles = 0
  31.   while clock < stop do
  32.     clock = os.clock()
  33.     cycles = cycles + 1
  34.   end
  35.   table.insert(cycleTable, cycles)
  36. end
  37.  
  38. cycleTotal = 0
  39. for k, v in pairs(cycleTable) do
  40.   cycleTotal = cycleTotal + v
  41. end
  42.  
  43. function round(value, place)
  44. return math.floor((value / place) + 0.5) * place
  45. end
  46.  
  47. cycleAverage = cycleTotal / cycleCount
  48. cycleAverage = round(cycleAverage, 0.01)
  49. hertz = cycleAverage * 20
  50. kilohertz = round((hertz / 1000), 0.01)
  51. megahertz = round((kilohertz / 1000), 0.01)
  52. ticks = cycleAverage
  53.  
  54. result("Hertz",hertz)
  55. result("Kilohertz",kilohertz)
  56. result("Megahertz",megahertz)
  57. result("Ticks",ticks)
  58.  
  59. print()
  60. setTextColor(colors.yellow)
  61. print("Press ENTER To Continue")
  62. print()
  63. setTextColor(colors.yellow)
  64. print("Raw Data (Hertz):")
  65. local ycount = 0
  66. local cursorx, cursory = term.getCursorPos()
  67. for k,v in pairs(cycleTable) do
  68.   if k == 1 then ycount = cursory end
  69.   if k == 6 then ycount = cursory end
  70.   if k == 11 then ycount = cursory end
  71.   if k == 16 then ycount = cursory end
  72.   if k > 15 then
  73.   term.setCursorPos(28,ycount)
  74.   elseif k > 10 then
  75.   term.setCursorPos(19,ycount)
  76.   elseif k > 5 then
  77.   term.setCursorPos(10,ycount)
  78.   elseif k > 0 then
  79.   term.setCursorPos(1,ycount)
  80.   end
  81.   setTextColor(colors.white) term.write(v)
  82.   setTextColor(colors.yellow) term.write("Hz")
  83.   ycount = ycount + 1
  84. end
  85.  
  86. while true do
  87.   event,key = os.pullEvent("key")
  88.   if key == 28 then break end
  89. end
  90.  
  91. term.clear()
  92. term.setCursorPos(1,1)
Advertisement
Add Comment
Please, Sign In to add comment