Guest User

modulo_benchmark.lua

a guest
Aug 9th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.58 KB | None | 0 0
  1.  
  2. local function benchmark()
  3.  
  4.    local N = 10000000
  5.    local k = 2
  6.  
  7.    local time_intervals = {}
  8.    for j = 1, 2*k-1 do
  9.       local two48 = 281474976710656  -- 2^48
  10.       local x
  11.       local clk0 = os.clock()
  12.       for n = 1, N do
  13.          x = two48
  14.             % 281633765821878
  15.             % 281861755493504
  16.             % 140964708094569
  17.             % 70513943648087
  18.             % 23510499215727
  19.             % 23530713002525
  20.             % 7844914056857
  21.             % 7845031723743
  22.             % 7847893636219
  23.             % 7849196854410
  24.             % 2616787772181
  25.             % 2616800002690
  26.             % 2617789799177
  27.             % 1309268885363
  28.             % 654833271837
  29.             % 655267877209
  30.             % 327691649351
  31.             % 327890975757
  32.             % 109326241594
  33.             % 54671377442
  34.             % 18228208084
  35.             % 6077944839
  36.             % 3039188803
  37.             --~ This sequence of constants was generated by the following code:
  38.             --~ local x = 2^48
  39.             --~ repeat
  40.             --~    local factor = math.random(3)
  41.             --~    local low = math.ceil(x / factor)
  42.             --~    local high = math.floor(x / (factor - 0.001))
  43.             --~    x = low + math.random(high - low)
  44.             --~    print('% '..x)
  45.             --~ until x < 2^32
  46.       end
  47.       time_intervals[j] = os.clock() - clk0
  48.    end
  49.  
  50.    -- get median time
  51.    table.sort(time_intervals)
  52.    print('CPU seconds for integer modulo:', time_intervals[k])
  53.  
  54.  
  55.    time_intervals = {}
  56.    for j = 1, 2*k-1 do
  57.       local two48 = 281474976710656.0  -- 2^48
  58.       local x
  59.       local clk0 = os.clock()
  60.       for n = 1, N do
  61.          x = two48
  62.             % 281633765821878.0
  63.             % 281861755493504.0
  64.             % 140964708094569.0
  65.             % 70513943648087.0
  66.             % 23510499215727.0
  67.             % 23530713002525.0
  68.             % 7844914056857.0
  69.             % 7845031723743.0
  70.             % 7847893636219.0
  71.             % 7849196854410.0
  72.             % 2616787772181.0
  73.             % 2616800002690.0
  74.             % 2617789799177.0
  75.             % 1309268885363.0
  76.             % 654833271837.0
  77.             % 655267877209.0
  78.             % 327691649351.0
  79.             % 327890975757.0
  80.             % 109326241594.0
  81.             % 54671377442.0
  82.             % 18228208084.0
  83.             % 6077944839.0
  84.             % 3039188803.0
  85.       end
  86.       time_intervals[j] = os.clock() - clk0
  87.    end
  88.  
  89.    -- get median time
  90.    table.sort(time_intervals)
  91.    print('CPU seconds for floating point modulo:', time_intervals[k])
  92.  
  93. end
  94.  
  95. benchmark()
Advertisement
Add Comment
Please, Sign In to add comment