# Is there a faster way to do this?

[code] dim test(999999,19) as string

for row as integer = 0 to 999999
for col as integer = 0 to 19
test(row, col) = "row: " + row.ToText + " col: " + col.ToText
next
next

It seems to take too much time… about 3 minutes on my pc

What if you replace the main line with:

``      test(row, col) = "row: " + str( row ) + " col: " + str( col )``

This runs in less than a minute: (old framework)

[code] dim test(999999,19) as string
dim row as variant
dim col as variant
row = 1

for row = 0 to 999999
for col = 0 to 19
test(row, col) = "row: " + row + " col: " + col
next
next[/code]

[code]Dim test(999999, 19) As String

For row As Integer = 0 to 999999
Dim rowString As String = "row: " + Str(row) + " col: "
For col As Integer = 0 to 19
test(row, col) = rowString + Str(col)
Next
Next

@Kem Tekinay great! much faster (1.13 minutes total). there will be an even faster way?

[code] Dim test(999999, 19) As String

Dim colString() As String

For col As Integer = 0 to 19
colString.Append(Str(col))
Next

For row As Integer = 0 to 999999
Dim rowString As String = "row: " + Str(row) + " col: "
For col As Integer = 0 to 19
test(row, col) = rowString + colString(col)
Next
Next

[code] dim test(999999,19) as string
dim row as variant
dim col as variant
dim s as string
row = 1

dim t as integer
t = ticks

for col = 0 to 19
s = " col: " + col
for row = 0 to 999999
test(row, col) = "row: " + row + s
next
next

15 to 20 seconds?

Machine code will be faster, but cost you seriously in dev time and with Xojo we are looking to sell a solution not our time (saw that around here somewhere). On my computer I can get this down to 9.35s, but that is within the margin of error, so @jean-paul devulder is probably as close as you can get.

Here’s a tip (10 - 30) seconds don’t matter if you show something happening, so show a progress bar - yes that will be slower, but your customer won’t notice the time at all BTW I learned that tip on this site and would pass credit if I could remember where it came from.

Wayne, I think that was from @Dave S .

Kem I think you might be right! Thanks @Dave S!

Here’s a modification of Jean-Pauls code that runs about 2.5 times faster (for me). The change is to precompute all the row strings.

edit: about the same as Elis second post

[code] #Pragma DisableBoundsChecking false

dim test(999999,19) as string
dim row as integer
dim col as integer
dim s,x as string
row = 1

dim t as integer
t = ticks

dim rowStrs(999999) As String
for row = 0 to 999999
rowStrs(row) = "row: " + str(row) + " col: "
next

for col = 0 to 19

``````s = " col: " + str(col)

for row  = 0 to 999999

test(row, col) = rowStrs(row) + s

next
``````

next

msgbox("time seconds: ") + format((ticks-t)/60,“0.00”)[/code]

Will, your code compiled in 32-bit takes about 5 seconds here. Well done.

With 64-bit (aggressive) it takes about 2.5 seconds.

I just “changed the conditions of the test” and used a MemoryBlock. The difference is, the col will be padded with spaces, for example:

``````row: 1  col: 1
row: 19 col: 100``````

On the other end, it won’t have have an encoding either.

But, as long as I pre-convert the numbers into strings, this runs about as fast as Will’s version, not faster, so I won’t bother posting it here.

One of the keys is to keep from converting the numbers to strings repeatedly. Will caches the bulk of that and it makes a huge difference.

Optimising was fun.
What it shows is that str() and especially .totext are expensive operations in a long loop

BUT
I suspect this doesnt have a practical application in its current form.

After all, what is the value of test(200,6) ?
Yup- its row: 200 col:6

You dont need to spend 3 minutes or 5 seconds filling the array.
You dont even need the array! just display a value when it is needed.

In a real application, the contents of this array would surely be ‘random’ data that doesnt lend itself to precalculation?