Last active
August 29, 2015 13:57
-
-
Save culurciello/9722698 to your computer and use it in GitHub Desktop.
torchFFI vs regular lua code: HUGE improvement
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- 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) |
Author
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
th -i testffi.lua
Time in lua: 0.22888493537903
Time with FFI: 0.0010042190551758