On Tue, Nov 18, 2014 at 3:24 PM, [email protected] <[email protected]>
wrote:

>
> Le 18 nov. 2014 22:52, "Sven Van Caekenberghe" <[email protected]> a écrit :
>
> >
> >
> > > On 18 Nov 2014, at 22:28, Andreas Wacknitz <[email protected]> wrote:
> > >
> > >
> > > Am 18.11.14 22:12, schrieb [email protected]:
> > >> See
> > >> https://news.ycombinator.com/item?id=8625280
> > >>
> > >> [ (1 to: 100000000) sum ] timeToRun
> > >>
> > >> Here we get out of the SmallInteger range...
> > >>
> > >> Apart FFI'ing the thing, do we have other options?
> > >>
> > >> Phil
> > > Choose a better algorithm:
> > > [100000000 * 10000001 / 2] timeToRun
> > > should beat yours by several orders of magnitude.
> > >
> > > Andreas
> >
> > Haha, great !
> >
> >
>
> Sure cool.
>
> Now Pharo is slow on these loops with arithmetics and it is painful.
>

Yes, but that particular loop is low because in part it uses the sum
convenience.  e.g.

Collection>>sum
"Compute the sum of all the elements in the receiver"

^self reduce:[:a :b| a + b]

Collection>>reduce: binaryBlock
"Apply the argument, binaryBlock cumulatively to the elements of the
receiver.
For sequenceable collections the elements will be used in order, for
unordered
collections the order is unspecified."

| first nextValue |
first := true.
self do: [ :each |
first
ifTrue: [ nextValue := each. first := false ]
ifFalse: [ nextValue := binaryBlock value: nextValue value: each ] ].
first ifTrue: [ self errorEmptyCollection ].
^nextValue

| n |
n := 100000000.
{  [ (1 to: n) sum ] timeToRun. [| s | s := 0. (1 to: n) do: [:i| s := s +
i]] timeToRun. [| s | s := 0. 1 to: n do: [:i| s := s + i]] timeToRun. [n *
(n + 1) / 2] timeToRun}

#(10112 9538 8432 0)

> Eliot, why is a class variable faster?
>

The expression is a constant.  You can e.g. compute it at class
 initialization and for descriptive purposes store the result in a class
variable. That's usually quicker than computing anything every time.
Standard examples are specialized dispatch tables, dictionaries, etc.
Class variables are a convenient way of holding onto precomputed objects.

Alex, I'll a look at OpenCL.
>
> Also, I tried with a whileTrue: loop which was 3 times faster than the sum
> on Collection.
>
> How to profile these kinds of tight loops? The standard profiler is kind
> of useless.
> Is AndreasProfiler the way to go
>
IMO yes.  It is much more accurate about primitives than the standard
profiler.
-- 
best,
Eliot

Reply via email to