Structs versus collection

I know nothing about C#. Could it be that those are more like Xojo structures than classes?

A C# class is much like a Xojo class it contains methods and properties. Don’t know how it operates under the hood though.

Here is the result with 1 million items.

I don’t know much about Xojo structs. What type of speed could you get with structs?

I don’t know as you’re not supposed to use them generally. But as data stores, they might be fine. Let me try…

I’m just thinking there has to be a Xojo equivalent that is at least close to this speed.

Adding 1M structures to a pre-dimensioned array takes about 150 ms. Adding 1M new instances of a class takes about 270 ms, so I’d say that part is comparable.

Without pre-dimensioning the array, both are around 300 ms with Structures still a bit faster.

Their big advantage to me would have been rapid copying of 100,000 of them at once to another ‘array’ memoryblock.
Milliseconds.
In all other particulars they would be slower (sorting, deleting, searching…)

I’m out for the weekend now but I’m going to do a bit more testing when I get back.

Adding the classes to an array is about just as fast but the ‘query’ takes 119 ms vs 4 ms in C#.

How to improve this part?

Dim items As Integer = 0 For i As Integer = 0 To lines.Ubound If lines(i).Startx = 99 And lines(i).starty > 50 Then items = items + 1 Next

Note: I didn’t realize using an array is that much faster than an in memory database.

Update from my project:
I switched the collections to dictionaries , and an operation that was taking 24 seconds is now less than 0.5 seconds.
Massive win!
I made hundreds of individual code changes to reach this point , so it’s heads down for testing over the next week or so.

In this specific case…

If that’s a query that’s needed all the time, create a Dictionary where the key is StartX. The Value is an array of WeakRefs of the object. Now you can grab just the items that match StartX and search through those. If you keep the resulting arrays sorted but StartY, you can search backwards and exit when StartY is below your given value.

If this is the only place you keep the objects, they can be straight-up arrays instead of WeakRefs.

OK after using aggressive compiling in 64 bit the query time is 24 ms. That’s close enough to satisfy me. :slight_smile: Now I have to rethink my apps that use in memory DB.

[quote=394252:@Neil Burkholder]Adding the classes to an array is about just as fast but the ‘query’ takes 119 ms vs 4 ms in C#.

How to improve this part?

Dim items As Integer = 0 For i As Integer = 0 To lines.Ubound If lines(i).Startx = 99 And lines(i).starty > 50 Then items = items + 1 Next

Note: I didn’t realize using an array is that much faster than an in memory database.[/quote]

I am having trouble with this… that code is linear… the more lines the longer it will take
But I’m still thinking an in-memory database, indexed properly would be faster (and linear only to the extent of the # of records that are in the criteria)

SELECT COUNT(8) FROM tablename WHERE startX=99 and StartY>0

Querying an in memory db isn’t terrible 64 ms for 1M records (still 3 times slower than using a for loop to iterate a class array). It’s adding the records that is do terribly slow. About 6.5 seconds versus about 300 ms to add 1 million class instances to an array.

Ok… right now I am bored… send me a copy of what you tested this with if you can… I’d be curious to see for myself.

Also… is your table INDEXED? adding records to a table with an existing index will be slower as it usually attempts to reindex multiple times depending on how you are doing the insert. Usually in production jobs we dropped the indexes, added the records, and reindexed again. And the speed was also dependant on how clustered the physical data was in comparison to the index criteria

OK I’m putting together a ‘presentable’ test app.

OK here you go https://www.dropbox.com/s/v7p2ejn3ocmplh5/Speed%20Test%20(2).xojo_binary_project?raw=1