WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Functions

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

FileWrite FilePut Benchmark

 Keywords: FileOpen FileWrite FilePut Optimize Benchmark BinaryWrite

Question:

Which is faster FileOpen / FileWrite versus FilePut?

Answer:

Generally your best bet is to open the file once usig FileOpen, loop using FileWrite in Append mode and call FileClose once at the end of the process.
GoSub Tests

loopcnt = 100
testfile = DirScript():'testlog.txt'
ret1 = udfTest1(loopcnt,testfile)
ret2 = udfTest2(loopcnt,testfile)
ret3 = udfTest3(loopcnt,testfile)
ret4 = udfTest4(loopcnt,testfile)

Message("Results", 'Test1 - Optimized = ': ret1 : @CR :'Test2 - Open and close =': ret2 : @CR :'Test3 - FilePut = ': ret3 : @CR :'Test4 - Binary = ': ret4  )
Exit

:TESTS
#DefineFunction udfTest1(loopcnt, testfile) ; Optimized
   start = GetTickCount()

   ;Add code here
   h = FileOpen(testfile, "append")
   For x = 1 To loopcnt
       FileWrite(h, "text for log")
   Next
   FileClose(h)
   FileDelete( testfile )

   endp = GetTickCount()
   elapsed = (endp - start)
   Return elapsed
#EndFunction

#DefineFunction udfTest2(loopcnt, testfile); Open and close
   start = GetTickCount()

   ;Add code here
   For x = 1 To loopcnt
       h = FileOpen(testfile, "append")
       FileWrite(h, "text for log")
       FileClose(h)
   Next
   FileDelete( testfile )


   endp = GetTickCount()
   elapsed = (endp - start)
   Return elapsed
#EndFunction

#DefineFunction udfTest3(loopcnt, testfile) ; FilePut
   start = GetTickCount()

   ;Add code here
   data = "text for log"
   For x = 1 To loopcnt
       FilePut( testfile, data )
       data = data:@CRLF:"text for log"
   Next
   FileDelete( testfile )

   endp = GetTickCount()
   elapsed = (endp - start)
   Return elapsed
#EndFunction


#DefineFunction udfTest4(loopcnt, testfile) ; Binary
   start = GetTickCount()

   ;Add code here
   data = "text for log"
   handle = BinaryAlloc((StrLen(data)+2)*100+100)
   offset = 0
   For x = 1 To loopcnt
       FilePut( testfile, data )
       BinaryPokeStr( handle, offset, "text for log":@CRLF )
       offset = offset +(StrLen(data)+2)
   Next
   BinaryWrite( handle, testfile )
   BinaryFree( handle )
   FileDelete( testfile )

   endp = GetTickCount()
   elapsed = (endp - start)
   Return elapsed
#EndFunction
Return

Article ID:   W17896
Filename:   FileWrite FilePut Benchmark.txt
File Created: 2014:04:22:10:43:20
Last Updated: 2014:04:22:10:43:20