lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Tue, Aug 26, 2008 at 6:17 AM, KHMan <keinhong@gmail.com> wrote:
> Alexander Gladysh wrote:

>> A little note: this is kind of slower way to check if string is empty.
>> Fastest would be to compare with empty string constant:
>>
>> for count = 1, math.huge do
>>   local line = io.read()
>>   if line == "" then break end
>>   io.write(string.format("%6d ", count), line, "\n")
>> end
>>
>> Please note that the difference is quite small though (note 10^9
>> iterations).
>
> Instead of such small differences, why not add an empty function call and
> then subtract out the function call overhead and loop overhead. Then we can
> compare the different comparisons in nanoseconds.

You're right, I've forgot to include noop function in tests list. Here
are the updated timings:

$ time lua estrbench.lua noop 1000000000
      109.36 real       106.91 user         0.47 sys


$ time lua estrbench.lua empty_constant 1000000000
      129.90 real       126.37 user         0.65 sys

$ time lua estrbench.lua empty_size 1000000000
      141.02 real       139.27 user         0.51 sys

$ time lua estrbench.lua empty_upvalue 1000000000
      142.28 real       133.89 user         0.90 sys


$ time lua estrbench.lua nonempty_constant 1000000000
      123.98 real       122.20 user         0.42 sys

$ time lua estrbench.lua nonempty_size 1000000000
      141.34 real       138.98 user         0.53 sys

$ time lua estrbench.lua nonempty_upvalue 1000000000
      174.01 real       163.54 user         1.18 sys


BTW, I have not included loop in my test cases. I should have
copy-pasted some examples in my letter, sorry. Here is the relevant
code:

local empty_string = ""

local noop = function() local a = "" return true end

local empty_constant = function()
  local a = ""
  return a == ""
end

local empty_upvalue = function()
  local a = ""
  return a == empty_string
end

local empty_size = function()
  local a = ""
  return #a == 0
end

local nonempty_constant = function()
  local a = "nonempty"
  return a == ""
end

local nonempty_upvalue = function()
  local a = "nonempty"
  return a == nonempty_string
end

local nonempty_size = function()
  local a = "nonempty"
  return #a == 0
end

Alexander.