AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

FileAppend Performance Concerns

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
Costas



Joined: 31 Mar 2009
Posts: 66

PostPosted: Fri Aug 28, 2009 5:27 pm    Post subject: FileAppend Performance Concerns Reply with quote

I love AHK and have found many uses for it. Recently, I decided to use it to add a header record to a number of HTML files that are automatically generated by another software tool.

AHK works great for this. The catch is that I need to read about 25 input files and some of these input files contain thousands of records.

I quickly learned that my AHK script would work, but it takes a long time to complete (over 30 minutes for all files).

I then did some performance tests.

If I have an input file of 3,000 records, I can read the entire file with AHK in less than one second (just read, no writes)

When I add the FileAppend command to write the records to a new file (to add my 1 new header record) the run time jumps to 30 seconds.

Is there a way to programmatically reduce this time? Am I missing something? Adding faster hardware is not an option due to budget constraints.

Thanks for your ideas,
Costas
Back to top
View user's profile Send private message
Leef_me



Joined: 08 Apr 2009
Posts: 5336
Location: San Diego, California

PostPosted: Fri Aug 28, 2009 6:13 pm    Post subject: Reply with quote

Quote:
Is there a way to programmatically reduce this time?
Can you post your script? Perhaps the group can spot an improvement. Exclamation

If that proves fruitless, you might be able to use a DOS level command (I haven't tried - - try seaching for 'comspec' in help)
Quote:

COPY [/D] [/V] [/N] [/Y | /-Y] [/Z] [/L] [/A | /B ] source [+ source [/A | /B] [+ ...]] [destination [/A | /B]]

To append files, specify a single file for destination, but multiple files for source (using wildcards or file1+file2+file3 format).
Back to top
View user's profile Send private message
[VxE]



Joined: 07 Oct 2006
Posts: 3254
Location: Simi Valley, CA

PostPosted: Fri Aug 28, 2009 7:09 pm    Post subject: Reply with quote

Using a Loop, Read, InputFile, OutputFile and using FileAppend inside the loop to append to the OutputFile will dramatically increase the speed of multiple FileAppends. See the (linked) docs for details.
edit:] link fix, sry about that.
_________________
Ternary (a ? b : c) guide     TSV Table Manipulation Library
Post code inside [code][/code] tags!


Last edited by [VxE] on Sat Aug 29, 2009 6:19 pm; edited 1 time in total
Back to top
View user's profile Send private message
Costas



Joined: 31 Mar 2009
Posts: 66

PostPosted: Sat Aug 29, 2009 1:42 am    Post subject: Reply with quote

Here is the code that I trimmed down for performance testing. After several tests, it appears that the FileAppend is the command that is very time consuming. Any ideas on how to speed this up would be most appreciated.

Thanks,
Costas

;~~~~~~~~~~~ START ~~~~~~~~~~~~~~~~~~~~~~~~

startTime:=A_TickCount

Input_File = C:\Input_File.TXT
Output_File = C:\Output_File.TXT

Filedelete %Output_File%

;~~~~~~~~
; Read input file and add a new record before the <HEAD> record as output file is created

loop, read, %Input_File%

{

IfInString, A_LoopReadLine, <HEAD>
{
FileAppend , Newly-Added-Test-Record `n, %Output_File%
}

FileAppend , %A_LoopReadLine% `n, %Output_File%

}


;~~~~~~~


EndTime:=A_TickCount

TimeInScript:=(A_TickCount-startTime)/1000
Msgbox Time in Script = %TimeInScript% seconds `n`n

EXITAPP

;~~~~~~~~~~~ END ~~~~~~~~~~~~~~~~~~~~~~~~
Back to top
View user's profile Send private message
horntail



Joined: 03 Aug 2009
Posts: 69
Location: UK

PostPosted: Sat Aug 29, 2009 1:56 am    Post subject: Reply with quote

test it again with this code, it should be faster and its what [VxE] meant in his post above:

Code:
startTime:=A_TickCount

Input_File = C:\Input_File.TXT
Output_File = C:\Output_File.TXT

Filedelete %Output_File%

;~~~~~~~~
; Read input file and add a new record before the <HEAD> record as output file is created

loop, read, %Input_File%, %Output_File%

{

IfInString, A_LoopReadLine, <HEAD>
{
FileAppend , Newly-Added-Test-Record `n
}

FileAppend , %A_LoopReadLine% `n

}


;~~~~~~~


EndTime:=A_TickCount

TimeInScript:=(A_TickCount-startTime)/1000
Msgbox Time in Script = %TimeInScript% seconds `n`n

EXITAPP
Back to top
View user's profile Send private message
Costas



Joined: 31 Mar 2009
Posts: 66

PostPosted: Sat Aug 29, 2009 12:24 pm    Post subject: Reply with quote

Horntail,

Thanks for the help, I really appreciate it.

I made this minor change as you suggested and the run time was reduced significantly.

Being a curious person, I would like to understand why this minor change made such a large impact.

Thanks again,
Costas
Back to top
View user's profile Send private message
why_bother_helping
Guest





PostPosted: Sat Aug 29, 2009 12:46 pm    Post subject: Reply with quote

Costas wrote:
Being a curious person, I would like to understand why this minor change made such a large impact.

Did you even read your own thread? The answer was provided by VxE here . Read the links he provided!! Evil or Very Mad
Back to top
Costas



Joined: 31 Mar 2009
Posts: 66

PostPosted: Sat Aug 29, 2009 1:23 pm    Post subject: Reply with quote

Sorry...

I did read what VXE posted, but I just didn't understand. I am still fairly new to AHK. The example that Horntail posted better helped me to understand what to try.

Now I would like to understand the underlying reason why this minor change makes such a big difference.

Thanks,
Costas
Back to top
View user's profile Send private message
Guest






PostPosted: Sat Aug 29, 2009 1:52 pm    Post subject: Reply with quote

Costas wrote:
Sorry...

I did read what VXE posted, but I just didn't understand. I am still fairly new to AHK. The example that Horntail posted better helped me to understand what to try.

Now I would like to understand the underlying reason why this minor change makes such a big difference.

Thanks,
Costas

You read this???
Loop (read file contents)
Quote:
Within the loop's body, use the FileAppend command with only one parameter (the text to be written) to append to this special file. Appending to a file in this manner performs better than using FileAppend in its 2-parameter mode because the file does not need to be closed and re-opened for each operation.
Back to top
Costas



Joined: 31 Mar 2009
Posts: 66

PostPosted: Sat Aug 29, 2009 4:41 pm    Post subject: Reply with quote

No, I had not read this. When I click on the first of the four links in the post that VXE added, I receive a "404 Page Not Found" error message.

The link that you show works fine and leads to the explanation of why this minor change is going to have such a large impact.

Thanks for the help, I really appreciate it.

Costas
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group