Skip to content

Instantly share code, notes, and snippets.

@p0pr0ck5
Created March 24, 2017 18:08
Show Gist options
  • Save p0pr0ck5/459d487e103fc92038f10be1918856a3 to your computer and use it in GitHub Desktop.
Save p0pr0ck5/459d487e103fc92038f10be1918856a3 to your computer and use it in GitHub Desktop.
┌──[poprocks@soter]─[~]
└── $ cat x.lua
ffi = require("ffi")
ffi.cdef[[
typedef long time_t;
typedef struct timeval {
time_t tv_sec;
time_t tv_usec;
} timeval;
int gettimeofday(struct timeval* t, void* tzp);
]]
gettimeofday_struct = ffi.new("timeval")
local function gettimeofday()
ffi.C.gettimeofday(gettimeofday_struct, nil)
return tonumber(gettimeofday_struct.tv_sec) * 1000000 + tonumber(gettimeofday_struct.tv_usec)
end
local v = require "jit.v"
v.on("/dev/stderr")
local ti = table.insert
local ok, tc = pcall(require, "table.clear")
local t = {}
local x = 0
local start
start = gettimeofday()
for i = 1, 10000000 do
t[#t + 1] = "foo"
end
print(gettimeofday() - start)
tc(t)
start = gettimeofday()
for i = 1, 10000000 do
ti(t, "foo")
end
print(gettimeofday() - start)
tc(t)
start = gettimeofday()
for i = 1, 10000000 do
x = x + 1
t[i] = "foo"
end
print(gettimeofday() - start)
┌──[poprocks@soter]─[~]
└── $ luajit ./x.lua
[TRACE 1 x.lua:33 loop]
[TRACE 2 (1/2) x.lua:34 -> 1]
1960384
[TRACE 3 x.lua:41 loop]
2091703
[TRACE 4 x.lua:49 loop]
10771
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment