完整与简单的i / o lua

时间:2015-06-29 21:42:31

标签: io lua

我正在尝试编写一个程序来分析模拟中的数据。由于我使用的模拟软件是运行Lua程序的,我不确定这是否是提出这个问题的正确位置,但我可能会编程错误。

我正在努力解决使用简单和完整I / O模型之间的区别。我有一段代码,它可以工作,看起来像这样:

io.output([[filename_and_location]]) 
function segment.other_actions
    if ion_splat ~= 0 then io.write(ion_px_mm, "\n") end
    io.close()
end

注意:ion_splat和ion_px_mm是带有数值的预定变量。该代码在整个模拟过程中反复运行。

然后我决定使用完整的I / O模型尝试实现相同的目标:

 f = io.open([[file_name_and_location]],"w")   
 function segment.other_actions ()
    if ion_splat ~= 0 then f:write(ion_py_mm, "\n") end
    f:close()
    end
end

这会运行,但批次比其他方式更长。这是为什么?

1 个答案:

答案 0 :(得分:0)

示例1:

for i = 1, 1000 do
   io.output("test.txt")
   io.write("some data to be written\n")
   io.close()
end

示例2:

for i = 1, 1000 do
   local f = io.open("test.txt", "w")
   f:write("some data to be written\n")
   f:close()
end

执行时间没有可测量的差异。

后一种方法通常更可取,因为明确标识了使用过的文件。