oliverk wrote:In my experience using put after url can be significantly slower than opening a file for append and writing to it.
Holy mackerel, Oliver! I ran a simple test to see just what the difference is, and it's quite astounding.
I ran this test:
Code: Select all
on mouseUp
-- Iterations to run:
put 10 into tIterations
put 1000 into tAppends
-- Setup data and file path:
put "xxxxxxxxxxxxxxxxxxxx" into tData
put "/Users/rg/Desktop/write_test1.txt" into tFile1
put "/Users/rg/Desktop/write_test2.txt" into tFile2
--
-- Test 1: url:
put the millisecs into t
repeat tIterations
PutFile1 tData, tFile1, tAppends
end repeat
put the millisecs - t into t1
--
-- Test 2: write:
put the millisecs into t
repeat tIterations
PutFile2 tData, tFile2, tAppends
end repeat
put the millisecs - t into t2
--
-- Show results:
put "URL: "&t1 &" ms Write:" & t2 &" ms"
end mouseUp
-- URL:
on PutFile1 pData, pFile, pAppends
if there is a file pFile then delete file pFile
repeat pAppends
put pData after url ("file:"& pFile)
end repeat
end PutFile1
-- write:
on PutFile2 pData, pFile, pAppends
if there is a file pFile then delete file pFile
open file pFile for text append
repeat pAppends
write pData to file pFile
end repeat
close file pFile
end PutFile2
So that's ten iterations in which each iteration writes 1000 appends to the file, resulting in a 20,000-byte file being written 10 times for each method.
That test gives me this result:
URL: 8094 ms Write:21 ms
I imagine that using the url form has the engine first reading the file, appending it in memory, then writing the whole file out to disk, while the write form simply writes to the end of the file without reading it first. Is that correct?
For quick one-offs the convenience of "put after url" is hard to beat, but when performance matters it definitely pays to write a couple extra lines to use the write command.