Dolphin is interpreted so will always be slower than a compiled language. This paper is rather old but still worth reading on the pros, cons and philosophy behind Dolphin's interpreter:
http://www.object-arts.com/downloads/papers/TheInterpreterIsDead.PDF
As a further comparison, running your test code in Pharo (JIT compiled) takes 2ms, compared to 18ms in Dolphin on my machine (I've changed the undefined 'height' for 800 in order to run the code).
In terms of optimisation, the Point arithmetic (position + speed) appears to be the main hotspot, possibly down to creating a new object. Reworking this to directly modify the existing position as follows:
each position x: each position x + each speed x.
each position y: each position y + each speed y.
...brings my runtime down to 12ms. This can be slightly reduced further (and the code tidied) by implementing a method in Point to do this:
moveBy: aPoint
x := x + aPoint x.
y := y + aPoint y
The test code then becomes:
each position moveBy: each speed.
...and runtime is 11ms.
A further optimisation is to use the shortcutting form of or: [..] instead of | in the conditional checks:
(((each position x + (32)) > 800) or: [(each position x + 32) < 0]) ifTrue: [each speed x: each speed x* -1].
(((each position y + (32)) > 800) or: [(each position y + 32) < 0]) ifTrue: [each speed y: each speed y* -1].
My final runtime is then 8ms.
HTH.
John
On Tuesday, September 8, 2020 at 11:55:48 PM UTC+1, Zenchess wrote:
Sorry about that, that wasn't actually the code that was taking a while, actually what I was doing in my code was:
Object subclass: #Bunny
instanceVariableNames: 'position speed color'
classVariableNames: ''
poolDictionaries: ''
classInstanceVariableNames: ''
(accessors for position, speed and color)
bunnies := OrderedCollection new.
1 to: 20000 do: [:index| |bunny|
bunny := Bunny new position: 200@200; speed: 5@5;yourself.
bunnies add: bunny.
].
and then my game loop would need to call the below to update them once per frame (of course I wasn't using time millisecondsToRun to run during the actual game loop)
Time millisecondsToRun:[bunnies do: [:each|
each position: each position + each speed.
((each position x + (32)) > 800) | ((each position x + 32) < 0) ifTrue: [each speed x: each speed x* -1].
(each position y + (32)) > height | ((each position y + 32) < 0) ifTrue: [each speed y: each speed y* -1].
]].
which takes 13 milliseconds on my dolphin (my processor is an i7 7700k). However when I profiled the same code in c++ it would only take about 200 microseconds.
Anyway, thanks for the responses, I will check the profiler in dolphin to see if I can optimize this.
--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)