Page 2 of 2

Re: Why is print() so expensive?

Posted: Tue Apr 14, 2015 12:40 pm
by Nixola
Well you could buffer manually:

Code: Select all

local oprint = print
print = function(...)
  oprint(...)
  io.stdout:flush()
end

Re: Why is print() so expensive?

Posted: Tue Apr 14, 2015 1:31 pm
by s-ol
Nixola wrote:Well you could buffer manually:

Code: Select all

local oprint = print
print = function(...)
  oprint(...)
  io.stdout:flush()
end
That's not faster though, is it?

Re: Why is print() so expensive?

Posted: Tue Apr 14, 2015 2:06 pm
by Nixola
It probably is if you set some output buffering, because it will spit out a lot of characters at once.
I tried using io.stdout:setvbuf 'no' and 'line' (automatically flushes when a newline is printed) and benchmarking them (100'000 print calls) noticing a 30% better performance with both a short string ("alpha") and a longer ( ("alpha"):rep(10)) string.