Put after URL vs. open/write/close

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
acidjazz
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 93
Joined: Mon Feb 16, 2009 2:37 am

Put after URL vs. open/write/close

Post by acidjazz » Tue Apr 28, 2009 1:48 am

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

gyroscope
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 404
Joined: Tue Jan 08, 2008 3:44 pm
Contact:

Post by gyroscope » Tue Apr 28, 2009 10:57 am

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.

:)

Klaus
Posts: 14198
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Post by Klaus » Tue Apr 28, 2009 6:27 pm

Sorry, not correct, both can be used with "file:" and "binfile:" :-)

gyroscope
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 404
Joined: Tue Jan 08, 2008 3:44 pm
Contact:

Post by gyroscope » Tue Apr 28, 2009 6:51 pm

Thank you for correcting me Klaus.

:)

acidjazz
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 93
Joined: Mon Feb 16, 2009 2:37 am

Post by acidjazz » Wed Apr 29, 2009 12:30 am

Okay, then, "put ... after URL.." wins, because no limitations, and it uses fewer lines of code. Thanks! - Mark P.

oliverk
Site Admin
Site Admin
Posts: 53
Joined: Mon Feb 27, 2006 2:16 pm

Post by oliverk » Tue May 05, 2009 12:14 pm

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
Oliver Kenyon
Software Developer
Runtime Revolution

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10050
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Post by FourthWorld » Tue May 05, 2009 4:24 pm

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.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Post Reply