Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/luajit
- local ffi = require('ffi')
- local length = 1000000
- local iterations = 1000
- ffi.cdef[[
- struct tm { uint64_t tv_sec; uint32_t tv_nsec; };
- int clock_gettime(int clockid, struct tm *r);
- int RAND_bytes(unsigned char *buf, int num);
- ]]
- local libopenssl = ffi.load('libcrypto')
- local CLOCK_PROCESS_CPUTIME_ID = 2
- local times = ffi.new('struct tm[2]')
- local function randomstr(len)
- local buffer = ffi.new('uint8_t[?]', len)
- libopenssl.RAND_bytes(buffer, len)
- return buffer
- end
- local srcstr = randomstr(length)
- local function searchmax(src, len)
- local counters = ffi.new('int32_t[256]')
- for i = 0, len - 1 do
- counters[src[i]] = counters[src[i]] + 1
- end
- for i = 255, 1, -1 do
- if counters[i] ~= 0 then
- return i
- end
- end
- return 0
- end
- local lmx = 0
- local rslt = {}
- ffi.C.clock_gettime(CLOCK_PROCESS_CPUTIME_ID, times[0]);
- for i = 1, iterations do
- rslt[i] = searchmax(srcstr, length)
- end
- ffi.C.clock_gettime(CLOCK_PROCESS_CPUTIME_ID, times[1]);
- print(string.format(
- "%d %f",
- rslt[1],
- tonumber(times[1].tv_sec - times[0].tv_sec)
- + (times[1].tv_nsec - times[0].tv_nsec) * 0.000000001))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement