Double check.
view end of the string this time.
Code: Select all
#include "crt/string.bi" 'needed for memcpy
#include "file.bi"
dim shared mfile as string
mfile="matrixdata.txt"
#define K_VALUE 10000 'can be increased for more speed
function clip_string_uni_matrix(z() as double) as string
' converts matrix to clips string variable, which is global
' terminate with crlf, also the last line
dim as integer lbx, ubx, lby, uby, i, j /' , nx, ny, nxy, nzsize '/
lbx = lbound(z , 1)
ubx = ubound(z , 1)
lby = lbound(z , 2)
uby = ubound(z , 2)
/' clips = "" '/
dim as zstring ptr ps1 = allocate(K_VALUE)
dim as integer k = K_VALUE
dim as integer max1 = 0
dim as integer ilen
dim as string temp
for i = lby to uby
for j = lbx to ubx
temp = str(z(i , j))
ilen = len(temp)
if k < max1 + 3 + ilen then
k *= 2
ps1 = reallocate(ps1 , k)
end if
memcpy(ps1 + max1 , strptr(temp) , ilen)
max1 += ilen
if j = ubx then
ps1[max1] = 13
ps1[max1 + 1] = 10
max1 += 2
else
ps1[max1] = 9
max1 += 1
end if
next j
next i
ps1[max1] = 0
return * ps1
end function
sub clip_string_uni_matrix2 (Z() As Double)
' converts matrix to clips string variable, which is global
' terminate with CRLF, also the last line
var f=freefile
Dim As Integer lbx, ubx, lby, uby, i, j, nx, ny, nxy, nzsize
lbx = Lbound(Z,1)
ubx = Ubound(Z,1)
lby = Lbound(Z,2)
uby = Ubound(Z,2)
open mfile for output as #f
For i = lby To uby
For j = lbx to ubx-1
print #1,str(Z(i,j))+ chr(9);
next j
print #1,Str(Z(i,ubx))
Next i
close #f
End sub
Function loadfile(file as string) as String
If FileExists(file)=0 Then Print file;" not found":Sleep:end
var f=freefile
Open file For Binary Access Read As #f
Dim As String text
If Lof(f) > 0 Then
text = String(Lof(f), 0)
Get #f, , text
End If
Close #f
return text
end Function
randomize 1
dim as long lim=511
redim z1(lim, lim) as double
dim as integer i, j
for i = 0 to lim
for j = 0 to lim
z1(i,j) = rnd() * 2145787
next
next
dim as double d1 = timer()
dim as string s1= clip_string_uni_matrix(z1())
d1 = timer() - d1
print "done in" , d1 ; " secondes" : print
print "len = " ;len(s1) : print : print
print "line endings ";instr(s1,chr(13,10))
print right(s1, 1000): print : print
print
print
d1=timer
dim as string s2
clip_string_uni_matrix2(z1())
s2=loadfile(mfile)
d1 = timer() - d1
print "done in" , d1 ; " secondes" : print
print "len = " ;len(s2) : print : print
print "line endings ";instr(s2,chr(13,10))
print right(s2, 1000): print : print
print iif(s2<>s1,"disagreement","OK")
print "Any key to close"
sleep
kill mfile