I don't think it'd be any faster than using arrays and the inbuilt array methods, it's always been the case that inbuilt data types are so much faster than anything we can assemble.Quote:
Originally Posted by Fall_X
Printable View
I don't think it'd be any faster than using arrays and the inbuilt array methods, it's always been the case that inbuilt data types are so much faster than anything we can assemble.Quote:
Originally Posted by Fall_X
I just remembered the reason, but it was before I found out about the Dictionary class.
I wanted to have a collection where I could easily say "delete object x". With an array, you'd have to loop through it first to find the right index in the array, then splice it, or keep an index stored in all the objects, but these would have to be reworked after splicing.
With a linked list, if I also stored a reference to the previous element, I would be able to take the previous element of the object, and update it's next-reference to the next-reference of the to-be-deleted object (and the other way round too, off course), and be done with it. But it would be a bit tedious, so I went for a dictionary instead.
Yeah, removal and insertion are LinkList's champion tools, but it's really a trade off.
What you gain when you remove something, is equalled to the speed you lose when you iterate it.
In that case, unless you're removing every frame, you can just iterate through the array till the correct element, then remove it.
Or you can leave a int in each object with its position in the array, and then when you remove it, you go to N to remove that object, and then iterate through the REST of the array, and decrement the position of the other objects.
Dictionaries are nice however, but they don't have a way to iterate through their members.
I think you can iterate through a dictionary using for...in, or the new for...each, can't you? I thought I tried that and it worked, but my memory might be playing tricks on me :)
Off course, but that would still depend on the size of the array. If it's large, it might not be an option.Quote:
Or you can leave a int in each object with its position in the array, and then when you remove it, you go to N to remove that object, and then iterate through the REST of the array, and decrement the position of the other objects.
Ah, for each works? I'm mistaken then, score one for not actually trying things :)
Yeah, it works (both for in and for each), just tried it again.
However, for dictionaries, you'll probably want to use for in, not for each. For each doesn't return the key, and because the keys in a dictionary are usually objects, you'll probably want the keys.
I tend to store a bunch of objects as keys, with a simple "true" as value. Not sure if it's a good idea, but it works for what I want to accomplish.
I'll try to do some benchmarking to see if dictionaries have a performance penalty compared to arrays.
Hmmm, this is weird. Doing 10000 sets of a for each and a for in on an array takes 94ms. Doing the same on a dictionary takes only 31ms. So unless I'm doing something wrong, it seems that a dictionary is about 3 times as fast as an array. That can't be right, can it? :S
Moved to AS3 forum.
Can I see your test code?Quote:
Originally Posted by Fall_X