Why is print() so expensive?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Why is print() so expensive?

Post by Nixola »

Well you could buffer manually:

Code: Select all

local oprint = print
print = function(...)
  oprint(...)
  io.stdout:flush()
end
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Why is print() so expensive?

Post 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?

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Why is print() so expensive?

Post 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.
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest