Sign in to follow this  
Followers 0
exodius

What's more efficient

6 posts in this topic

I've been wondering, what's more efficient for reading the contents of a file?

1. Reading a file to an array using _FileReadToArray ( "file.txt", $array )

2. $file = FileOpen ( File.txt", 0 )

3. Something else I'm not thinking of?

Share this post


Link to post
Share on other sites



I've been wondering, what's more efficient for reading the contents of a file?

1. Reading a file to an array using _FileReadToArray ( "file.txt", $array )

2. $file = FileOpen ( File.txt", 0 )

3. Something else I'm not thinking of?

Well 1 and 2 do not do comparable things.

Your "option 2" returns the handle of the opened file.

If you meant 2. $file = FileRead...

then

It depends on what you want to do with the info. If you just want to check to see if a string exists in the file, then you might not need to place it into an array.

If you want/need the info in an array and if you are going to check @error and the return value from _FileReadToArray - then it is best to use it. If you are not going to check the values set by the UDF, then don't use it. You can just use the one line of code that is the core of that UDF.

$aArray = StringSplit(FileRead("File.txt")), @LF)

not tested, but requires beta


[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites

Well 1 and 2 do not do comparable things.

Your "option 2" returns the handle of the opened file.

If you meant 2. $file = FileRead...

then

It depends on what you want to do with the info. If you just want to check to see if a string exists in the file, then you might not need to place it into an array.

If you want/need the info in an array and if you are going to check @error and the return value from _FileReadToArray - then it is best to use it. If you are not going to check the values set by the UDF, then don't use it. You can just use the one line of code that is the core of that UDF.

$aArray = StringSplit(FileRead("File.txt")), @LF)

not tested, but requires beta

My intention anytime I do this is to read the contents of the file, parsing out the text that I need... so with that detail in mind... :D

Share this post


Link to post
Share on other sites

...read the contents of the file, parsing out the text that I need...

Sorry, I don't mean to be a pain, but "parsing out" is not enough info of what you want to do for me to comment on your question... and now that I think about it, I'm going to assume that efficient means the fastest executing code and not quickest for you to write.

If you have some HTML that you want to "parse out" part of the body, then I would not read the info to an array. I would read it into a variable and then trim left and right using StringInStr.

You might want to post a little sample of the data that you want to read in and then explain what you want to do with it.

No one method is going to be the most efficient for all situations.


[size="1"][font="Arial"].[u].[/u][/font][/size]

Share this post


Link to post
Share on other sites

StringRegExp() is generally better and more efficient if it is specific text you want to find.


[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0