New topic: text write file - speed optimization.
<http://forums.realsoftware.com/viewtopic.php?t=46391> Page 1 of 1 [ 4 posts ] Previous topic | Next topic Author Message Antonio Post subject: text write file - speed optimization.Posted: Sun Dec 30, 2012 12:27 pm Joined: Sat Feb 04, 2012 1:26 pm Posts: 65 Location: Italy Hi, I used the following code to write a series of consecutive numbers inside a text file. Is there the possibility to write a code that perform the same with faster execution speed ? Dim t as TextOutputStream Dim f as FolderItem dim conta as integer f=GetFolderItem("write.txt") If f <> Nil then t=TextOutputStream.create(f) for conta = 1 to 1000000 t.WriteLine (str(conta)) next conta msgbox "finito" t.Close End if Best Regards, Antonio Top ktekinay Post subject: Re: text write file - speed optimization.Posted: Sun Dec 30, 2012 12:59 pm Joined: Mon Feb 05, 2007 5:21 pm Posts: 328 Location: New York, NY Because of caching, that might be plenty fast, but here is the other, potentially faster, but less memory-efficient, way to do it: dim highNumber as integer = 1000000 dim arr() as String redim arr( highNumber ) // One more than we need to preserve the trailing EOL for conta as integer = 1 to highNumber arr( conta - 1 ) = str( conta ) next Dim t as TextOutputStream Dim f as FolderItem f=GetFolderItem("write.txt") If f <> Nil then t=TextOutputStream.create(f) if t <> Nil then t.Write join( arr, EndOfLine ) msgbox "finito" t.Close end if End if _________________ Kem Tekinay MacTechnologies Consulting http://www.mactechnologies.com/ Need to develop, test, and refine regular expressions? Try RegExRX. Top Antonio Post subject: Re: text write file - speed optimization.Posted: Sun Dec 30, 2012 2:13 pm Joined: Sat Feb 04, 2012 1:26 pm Posts: 65 Location: Italy Kem, your code is incredibly faster about 1 second against 10 seconds, previous achieved. I have tried to understand the code, analyzing what has locked the speed, earlier. The great improvements seems to be related to the use of arrays. What I understood is that , in your code, the cycle "for next" is workedout inside the memory where the list of number is stored. Then array is entirely written (and not line by line which takes more time) to the file. that's why the use of write + endofline instead of writeline. Top ktekinay Post subject: Re: text write file - speed optimization.Posted: Sun Dec 30, 2012 2:46 pm Joined: Mon Feb 05, 2007 5:21 pm Posts: 328 Location: New York, NY Yes, doing as much as you can in RAM before writing to a file will generally make things faster. This assumes, however, that you have the RAM available. If you are dealing with a very large string and your app has to start using virtual memory, I'd expect that method to be slower. Concatenating a string using Join( array ) is faster than str = str + newStr. The reason is that strings are immutable. They exist or not, but can never be changed, so if you try this code: s = "string1" a = "string2" s = s + a What's really happening is that a string, "string1" is created in memory. Another string, "string2" is also created, then a third string, "string1string2" is created, and, because there are no more references to it, "string1" is destroyed. This all takes time, and it will add up if you have to do this a million times. Instead, this code: dim arr() as string arr.Append "string1" arr.Append "string2" s = join( arr, "" ) Will do the same thing, but will be faster at it since the individual strings are created and assigned to the array, then the final string is created just once. (It will also take at least twice the RAM.) If you need something between them, like an EndOfLine, it is faster still: s = Join( arr, EndOfLine ) instead of: s = s + a + EndOfLine _________________ Kem Tekinay MacTechnologies Consulting http://www.mactechnologies.com/ Need to develop, test, and refine regular expressions? Try RegExRX. Top Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending Page 1 of 1 [ 4 posts ] -- Over 1500 classes with 29000 functions in one REALbasic plug-in collection. The Monkeybread Software Realbasic Plugin v9.3. http://www.monkeybreadsoftware.de/realbasic/plugins.shtml [email protected]
