Page 1 of 1
Put after URL vs. open/write/close
Posted: Tue Apr 28, 2009 1:48 am
by acidjazz
Dear Rev Users,
I've got data stored in gDataSet and want to append to the end of a textfile. Are there any advantages to using "open for append/write/close" instead of the much simpler "put after URL" approach. Both approaches seem to work, but the latter uses only a single line of code. -- Thanks, Mark P.
Approach 1
Code: Select all
on mouseUp
open file "data.txt" for append
write gDataSet to file "data.txt"
close file "data.txt"
end mouseUp
Approach 2
Code: Select all
on mouseUp
put gDataSet after URL "file:data.txt"
end mouseUp
Posted: Tue Apr 28, 2009 10:57 am
by gyroscope
Hi acidjazz
I think I'm correct in saying that with approach 1 you can open a text or a binary file but with approach 2, that can be used with a text file only.

Posted: Tue Apr 28, 2009 6:27 pm
by Klaus
Sorry, not correct, both can be used with "file:" and "binfile:"

Posted: Tue Apr 28, 2009 6:51 pm
by gyroscope
Thank you for correcting me Klaus.

Posted: Wed Apr 29, 2009 12:30 am
by acidjazz
Okay, then, "put ... after URL.." wins, because no limitations, and it uses fewer lines of code. Thanks! - Mark P.
Posted: Tue May 05, 2009 12:14 pm
by oliverk
Hi,
In my experience using put after url can be significantly slower than opening a file for append and writing to it. Often this is not a problem, and the extra simplicity of the url approach wins. But its worth bearing in mind, particularly with larger files, as it could cause issues.
Regards
Oliver
Posted: Tue May 05, 2009 4:24 pm
by FourthWorld
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.