:oops: I'm working on it, slowly but I'm working.
Printable View
:oops: I'm working on it, slowly but I'm working.
I have another suggest for those who have a homogeneous array of numbers that's elements count is unknown in advance but the size altogether will not exceed 2GB.
No redim needed if elements shall be added, all done by the powers of thinBasic.
Use a string.
How?
Just an idea. Typed this on the mobile, not error-checked. Perhaps the $-sign is wrong on the phone? If so someone may correct it.Code:Uses "console"
' this will store the numbers:
Dim myNumericArray as String
' access the numbers virtually:
Dim myNumber() as Number At 0
Dim i As Long ' just to list our arrays elements
' add a few elements, type Ext equals Number, that's why MKE$:
myNumericArray = MKE$(0.12345) ' the first element be 0.12345
' just append a bunch of new elements:
myNumericArray &= MKE$(1,2,3,4,5)
' and some more
myNumericArray &= MKE$(6.666, 7.777)
' now let's read what we have:
' put the virtual array onto the string:
Redim myNumber(Strptrlen(Strptr(myNumericArray))/SizeOf(Number)) At Strptr(myNumericArray)
' list what we have
For i = 1 to UBound(myNumber)
PrintL i, myNumber(i)
Next
WaitKey
But you could tell me if string reallocates faster than Redim :)
Rene,
thank you for your example! This approach could be further mixed with stringBuilder to reach maximum efficiency.
Each &= means reallocation in your case, while stringBuilder reallocates only when you reach its internal capacity, which keeps doubling :)
Petr
@Petr Schreiber,
Could you please post an example with StringBuilder ?
@kcvinu
until Petr reply, i say i have tried string builder just recently, and it is very efficient, here its thread http://www.thinbasic.com/community/s...inBASIC-GitHub
look the Petr example there
while its page is here: https://github.com/petrSchreiber/thi...ngBuilder/wiki
look more of its methods available
it is genuine and in the github page is its source code
to create it:
and to build big string from some wordsCode:'Dim stringBuffer As String
Dim sb As StringBuilder
sb = New StringBuilder()
the result is instantaneousCode:For i = 1 To 20000
sb.Add("Appending is fine!")
Next
here is we save the big string to a file because the Console can't display more than a few thousands of charaters)
note what in the string builder we copy to a usual string
bigString = sb.ToString() to display it.
Code:Uses "StringBuilder", "Console", "File"
Function TBMain()
'Dim stringBuffer As String
Dim bigString As String
Dim sb As StringBuilder
sb = New StringBuilder()
Long i
For i = 1 To 20000
sb.Add("Appending is fine!")
Next
bigString = sb.ToString()
FILE_Save("test.txt", bigString)
PrintL "big string saved as file test.txt .... Press any key to quit..."
WaitKey
End Function
@primo,
Thanks for the example. Its really fast for creating and saving that 352 KB text file. I think the variable named Stringbuffer is useless in your code. Let me check the links you provide. Thanks again. :)
Thank you kcvinu for the notes.
i have tried also to loop up to a million loops for the sentence of 20 characters. and using AddLine method (listed in page https://github.com/petrSchreiber/thi...ngBuilder/wiki ) which adds a new line after reading one string. in one second it saves about 21 MB txt file.
but never try to open this big text file in notepad it will hang your computer. a good editor i see to open these big files is EmEditor
https://www.emeditor.com/text-editor...s-up-to-248gb/
but the notepad++ can open it ( https://notepad-plus-plus.org/download/ ) note that opening the files which have many new lines is easier than continuous file.
there are other things to discover yet. thanks for Petr and Eros for this jewel .Code:Uses "StringBuilder", "Console", "File"
Function TBMain()
'Dim bigString As String
Dim sb As StringBuilder
sb = New StringBuilder()
Long i
For i = 1 To 1000000
sb.AddLine("Appending is fine!!!")
Next
'bigString = sb.ToString()
FILE_Save("test.txt", sb.ToString())'bigString)
PrintL "big string saved as file test.txt .... Press any key to quit..."
WaitKey
End Function
@primo,
Wow ! one million lines of text. Obviously it's a huge file. I am assuming that you might see your computer hung when you opened it in notepa. Am i right ?
in windows xp the notepad does not hung if we created the txt file using sb.AddLine even with slow performance , but it hung and freeze if we created the file with sb.Add ie as one line. to be sure the end of the file displayed add "finish":Quote:
kcvinu wrote: I am assuming that you might see your computer hung when you opened it in notepa. Am i right ?
Attachment 9663
For i = 1 To 1000000
sb.Add("Appending is fine!!!")
Next
sb.Add("finish")
the universal viewer is able to load any file
the freeware version of universal viewer is here:
http://www.uvviewsoft.com/uviewer/download.htm
but only EmEditor is able to show the 20 MB one line file as one line, when we move the cursor to the end of the file we see this
ln 1 col 20000006
ie line 1 column 20,000,006
@primo,
Thanks a lot for introducing Universal Viewer. Its a must have program. :)