Skip to content

Instantly share code, notes, and snippets.

@culurciello
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save culurciello/9722698 to your computer and use it in GitHub Desktop.

Select an option

Save culurciello/9722698 to your computer and use it in GitHub Desktop.
torchFFI vs regular lua code: HUGE improvement
-- test of FFI vs regular lua code
-- EC march 21st 2014
torch.setdefaulttensortype('torch.FloatTensor')
require 'sys'
require 'image'
require 'torchffi'
local bit = require('bit')
local ffi = require('ffi')
local C = ffi.C
-- blob parser LUA:
function parse(tin, threshold, blobs, scale)
--loop over pixels
for y=1, tin:size(1) do
for x=1, tin:size(2) do
local val = tin[y][x]
if (val > threshold) then
entry = {}
entry[1] = x
entry[2] = y
entry[3] = scale
table.insert(blobs,entry)
end
end
end
end
-- blob parser FFI:
function parseFFI(tin, threshold, blobs, scale)
--loop over pixels
for y=0, 511 do
for x=0, 511 do
local val = tin[512*y+x]
if (val > threshold) then
entry = {}
entry[1] = x
entry[2] = y
entry[3] = scale
table.insert(blobs,entry)
end
end
end
end
-- TEST:
tima = image.lena()[1]
timap = torch.data(tima)
sys.tic()
parse(tima, 10, 0, 0)
t = sys.toc()
print('Time in lua: ', t)
sys.tic()
parseFFI(timap, 10, 0, 0)
t = sys.toc()
print('Time with FFI: ', t)
@Atcold
Copy link

Atcold commented Mar 29, 2014

Basically, you are sending a 2D Tensor in the first case and and an array pointer in the second. Is it correct? So, there is a 230× improvement??? :o
And, ffi is the luajit thing, correct?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment