[q]squeak is too slow?

Yoshiki Ohshima Yoshiki.Ohshima at acm.org
Wed Dec 8 04:56:36 UTC 2004

Hello,

> 'Something' is just array manipulations.
> Asuume that (j,k) are a matrix(30 by 30).
> For each (j,k) I want to compute sum of distance from (j,k) to all others.
> Finally we have to get a minimum among sums.

You still didn't tell us how your code exactly looks like (why?), but
is it something you can do with the following?

| a b sum xDiff yDiff sq r |
a _ (1 to: 30) inject: (FloatArray new: 0) into: [:s :t | s, ((1 to: 30) as: FloatArray)].
b _ (1 to: 30) inject: (FloatArray new: 0) into: [:s :t | s, (FloatArray new: 30 withAll: t)].
r _ FloatArray new: 900.
1 to: 30 do: [:j |
1 to: 30 do: [:k |
sum _ 0.0.
xDiff _ a - j asFloat.
yDiff _ b - k asFloat.
sq _ (xDiff * xDiff) + (yDiff * yDiff).
sq _ sq collect: [:x | x sqrt].
sum _ sq sum.
r at: ((j - 1) * 30) + k put: sum.
].
].
^ r min.

(Set up 'a and 'b' if you need to put some values, but shouldn't
affect the performance.)

This runs in about 1.5 sec on my computer.  Interestingly, more than
95 percent of time goes to

sq _ sq collect: [:x | x sqrt].

line, so if you comment this line out, it runs in about 60ms.  If you
change the algorithm to avoid the square root calculation, it should
run in around 60ms.  (If you have a modestly fast computer.)

Note that the above code allocates a lot of redundant arrays.  One
can imagine to omit these allocation.

By the way, ... what kind of computer you're using, and how the code
you tried exactly look like?  If you don't tell us that, we can't
discuss any more interesting stuff.  A very, very naive version I
tried runs in about 4 sec, so still something might be written poorly
in your code, or the computer is slow from today's standard, or
something like that.

-- Yoshiki

P.S.
I like to abuse the FloatArray and friends...