[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Ternary slower than function call with if statement?
- From: Tristan Kohl <tristankohl@...>
- Date: Mon, 11 Jul 2022 18:05:02 +0200
Yes, you are absolutely right. Changing the ternary it is now faster
without calling the function.
Thanks a lot!
On 11.07.22 17:54, Xmilia Hermit wrote:
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)