lua-users home
lua-l archive

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


> -----Original Message-----
> From: lua-l-bounces@lists.lua.org [mailto:lua-l-bounces@lists.lua.org] On
> Behalf Of Eike Decker
> Sent: woensdag 26 juni 2013 21:54
> To: Lua mailing list
> Subject: Re: write a list of variables to a file, formatted
> 
> 
> Am 26.06.2013 17:06 schrieb "Daniel Barna" <daniel.barna@cern.ch>:
> >
> > Hi, I have a function which return a list of variables:
> > function some_function()
> >   return a,b,c,d;
> > end
> >
> > I would like to write this to a file, using a whitespace as the
> separator, something like this:
> > some_file:write(some_function());
> >
> > How can I do this?
> > Thank you
> > Daniel
> >
> How about this:
> function concat(sep,...)
> 	local t = {}
> 	for i=1,select('#',...) do
> 		t[i] = tostring(select(i,...))
> 	end
> 	return table.concat(t,sep)
> end
> 
> function test() return 1,nil,2,nil,nil end
> print(concat(", ",test()))


-- use from 5.2 or define for 5.1
local pack = table.pack or function(...) return {n = select("#", ...), ...} end

function concat(sep,...)
	local t = pack(...)
	for i=1, t.n do
		t[i] = tostring(t[i])
	end
	return table.concat(t, sep)
end

Nitpicking, but this is slightly faster, probably because of lesser use of varargs (...) (see test below)
Thijs

> 
> As pointed out before by Thijs Schreijer, wrapping arguments into tables
> causes trouble when nil values are around. Besides, nonstring values can
> also be problematic with concat. I am not sure, but the code I wrote
> should handle both cases correctly.
> 
> Cheers,
> Eike

Timed test:
=============
local time = require('socket').gettime

-- use from 5.2 or define for 5.1
local pack = table.pack or function(...) return {n = select("#", ...), ...} end

function concat1(sep,...)
	local t = pack(...)
	for i=1, t.n do
		t[i] = tostring(t[i])
	end
	return table.concat(t, sep)
end

function concat2(sep,...)
 	local t = {}
 	for i=1,select('#',...) do
 		t[i] = tostring(select(i,...))
 	end
 	return table.concat(t,sep)
end

function test() return 1,nil,2,nil,nil end
function exec(f,n)
  for i = 1, n do local x = f(", ",test()) end
end

function runner(n)
  local t1 = time()
  exec(concat1,n)
  local t2 = time()
  exec(concat2,n)
  local t3 = time()

  print(n.." iterations")
  print("   concat1: ", t2-t1)
  print("   concat2: ", t3-t2)
  print("   diff: ", ((t3-t2)/(t2-t1) - 1) * 100, "% slower")
end
runner(1000)
runner(10000)
runner(100000)
runner(1000000)

Results:
1000 iterations
   concat1: 	0
   concat2: 	0
   diff: 	-1.#IND	% slower
10000 iterations
   concat1: 	0.046800136566162
   concat2: 	0.062400102615356
   diff: 	33.333163520026	% slower
100000 iterations
   concat1: 	0.4212007522583
   concat2: 	0.59280109405518
   diff: 	40.740749126593	% slower
1000000 iterations
   concat1: 	6.9420120716095
   concat2: 	8.7672154903412
   diff: 	26.292138358505	% slower
Program completed in 16.91 seconds (pid: 8024).