Advertisement
suremarc

Untitled

Sep 14th, 2014
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.82 KB | None | 0 0
  1. --[[
  2. ----------
  3. Time taken: 0.38085579872131s
  4. Reps: 1000000
  5. Average time: 380ns
  6. ----------
  7. Time taken: 0.1891758441925s
  8. Reps: 1000000
  9. Average time: 189ns
  10. ]]
  11. function floorIt()
  12.     local n = math.random()*2;
  13.     return math.floor(n) == 0;
  14. end
  15.  
  16. function almostZero()
  17.     local n = math.random()*2;
  18.     return n < 1e-2;
  19. end
  20.  
  21. unitValue = {
  22.     ms = 1e3;
  23.     us = 1e6;
  24.     ns = 1e9;
  25. }
  26. function secondsTo(unit, numSecs)
  27.     return ""..math.floor(numSecs*unitValue[unit])..unit;
  28. end
  29.  
  30. function timeTest(unit, f, reps, ...)
  31.     local ti = tick();
  32.     for i = 1, reps do
  33.         f(...);
  34.     end
  35.     local t_total = tick() - ti;
  36.     print('----------');
  37.     print("Time taken: "..t_total.."s");
  38.     print("Reps: " .. reps);
  39.     print("Average time: "..secondsTo(unit, t_total/reps));
  40. end
  41.  
  42. timeTest('ns', floorIt, 1000000);
  43. timeTest('ns', almostZero, 1000000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement