lua-users home
lua-l archive

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


Tristan Kohl wrote:

I was very surprised to see that a ternary is significantly
slower than a function call with an if statement inside.

Maybe this is caused by the wrong negation of the condition.

Comparing the condition in

if #lastChunk < chunkSize then
   return file:read(chunkSize)
else
   return ""
end

and

local chunk = #lastChunk > chunkSize and "" or file:read(chunkSize)

one can notice that (not(#lastChunk < chunkSize)) ~= (#lastChunk > chunkSize). The condition #lastChunk > chunkSize in the trinary case will more often fail since #lastChunk is linkely chunkSize after the first read and therefore read even more. This overhead might cause the observed behaviour.

Maybe test with

local chunk = #lastChunk >= chunkSize and "" or file:read(chunkSize)

Regards,
Xmilia