[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Ternary slower than function call with if statement?
- From: Xmilia Hermit <xmilia.hermit@...>
- Date: Mon, 11 Jul 2022 17:54:58 +0200
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
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
Maybe test with
local chunk = #lastChunk >= chunkSize and "" or file:read(chunkSize)