Advertisement
Guest User

Untitled

a guest
Feb 10th, 2021
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.19 KB | None | 0 0
  1. #!/usr/bin/luajit
  2. local ffi = require('ffi')
  3.  
  4. local length     = 1000000
  5. local iterations = 1000
  6.  
  7. ffi.cdef[[
  8. struct tm { uint64_t tv_sec; uint32_t tv_nsec; };
  9. int clock_gettime(int clockid, struct tm *r);
  10. int RAND_bytes(unsigned char *buf, int num);
  11. ]]
  12.  
  13. local libopenssl = ffi.load('libcrypto')
  14.  
  15. local CLOCK_PROCESS_CPUTIME_ID = 2
  16. local times = ffi.new('struct tm[2]')
  17.  
  18. local function randomstr(len)
  19.     local buffer = ffi.new('uint8_t[?]', len)
  20.     libopenssl.RAND_bytes(buffer, len)
  21.     return buffer
  22. end
  23.  
  24. local srcstr = randomstr(length)
  25.  
  26. local function searchmax(src, len)
  27.    local counters = ffi.new('int32_t[256]')
  28.    for i = 0, len - 1 do
  29.       counters[src[i]] = counters[src[i]] + 1
  30.    end
  31.    for i = 255, 1, -1 do
  32.      if counters[i] ~= 0 then
  33.        return i
  34.      end
  35.    end
  36.    return 0
  37. end
  38.  
  39. local lmx  = 0
  40. local rslt = {}
  41.  
  42. ffi.C.clock_gettime(CLOCK_PROCESS_CPUTIME_ID, times[0]);
  43. for i = 1, iterations do
  44.    rslt[i] = searchmax(srcstr, length)
  45. end
  46. ffi.C.clock_gettime(CLOCK_PROCESS_CPUTIME_ID, times[1]);
  47.  
  48. print(string.format(
  49.    "%d %f",
  50.     rslt[1],
  51.     tonumber(times[1].tv_sec - times[0].tv_sec)
  52.     + (times[1].tv_nsec - times[0].tv_nsec) * 0.000000001))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement