[Pharo-dev] #sum:, #detectSum:, #sumNumbers:

Nicolai Hess nicolaihess at gmail.com
Tue Dec 22 06:06:12 EST 2015


2015-12-20 12:59 GMT+01:00 Max Leske <maxleske at gmail.com>:

>
>
> I propose to use Sven’s version for #sum:ifEmpty:. The result would be
> these three methods:
>
> sum
> ^ self
> sum: [ :each | each ]
> ifEmpty: [ 0 ]
>
> sum: aBlock
> ^ self
> sum: aBlock
> ifEmpty: [ self errorEmptyCollection ]
>
> sum: aBlock ifEmpty: emptyBlock
> | sum sample |
> self isEmpty ifTrue: [ ^ emptyBlock value ].
> sample := self anyOne.
> sum := self
> inject: sample
> into: [ :accum :each | accum + (aBlock value: each) ].
> ^ sum - sample
>
>
> I’ve attached a couple of benchmark results below. To me they show that
> 1. the new implementation is maybe a tiny bit slower but insignificantly
> so (if you’re going for performance you’ll probably write your own
> optimised version anyway)
> 2. there is no need to duplicate the code (like #sum and #sum: currently
> do). It’s perfectly fine to delegate to #sum:ifEmpty:
>
>
>
> In addition to the above changes I would like to remove #detectSum: (->
> #sum:) and #sumNumbers (-> #sum).
>

there is no #sumNumbers, only #sumNumbers:


>
>
> Note that once we agree on changing this API, we will need to also change
> #detectMin:, #detectMax:, #min, #max as well as all overrides (e.g.
> RunArray, Interval) of these and of #sum et. al. to stay consistent. The
> changes would of course be in line with this change, such that every
> operation has a unary selector with a sensible default, one that takes a
> block and throws an error for empty collections and a third that takes a
> block for the iteration and one for the empty case.
>
>
> Please cast your vote for these changes:
>
> 1. Do you agree to changing #sum and #sum: in the suggested way?
>

yes


>
> 2. Do you agree to the removal of #detectSum:?
>

yes


>
> 3. Do you agree to the removal of #sumNumbers?
>

yes


>
> 4. Do you agree that the #max and #min selectors also need to be adapted?
>

hm. I like the "detect". If

aCollection detectMax: aBlock
becomes

aCollection max: aBlock

it looks too similar  to
aCollection size max: aValue

But  I could live with that :)


>
>
> Thanks for you help.
>
> Cheers,
> Max
>
>
>
>
>
> Benchmarks
> ============
> (Note that these aren’t very good benchmarks. There’s quite some variation
> on each run.)
>
>
> Machine:
> MacBook Pro (15-inch, Early 2011)
> CPU: 2.2 GHz Intel Core i7
> Memory: 8 GB 1333 MHz DDR3
> Disk: APPLE SSD TS512C (500 GB)
>
>
> Benchmarks of the current versions:
>
> [ (1 to: 1000000) asArray sum ] benchFor: 10 seconds.
> 75 iterations, 7.470 per second
>
> [ (1 to: 1000000) asArray sum: [ :e | e ] ] benchFor: 10 seconds.
> 72 iterations, 7.128 per second
>
>
> [ (1 to: 100) asArray sum ] benchFor: 10 seconds.
> 1,189,477 iterations, 118,912 per second
>
>
> [ (1 to: 100) asArray sum: [ :e | e ] ] benchFor: 10 seconds.
> 1,171,467 iterations, 117,112 per second
>
>
>
> Benchmarks of the new versions:
>
> [ (1 to: 1000000) asArray sum ] benchFor: 10 seconds.
> 73 iterations, 7.244 per second
>
> [ (1 to: 1000000) asArray sum: [ :e | e ] ] benchFor: 10 seconds.
> 75 iterations, 7.480 per second
>
> [ (1 to: 1000000) asArray sum: [ :e | e ] ifEmpty: [ 0 ] ] benchFor: 10
> seconds.
> 72 iterations, 7.141 per second
>
>
> [ (1 to: 100) asArray sum ] benchFor: 10 seconds.
> 1,115,827 iterations, 111,560 per second
>
> [ (1 to: 100) asArray sum: [ :e | e ] ] benchFor: 10 seconds.
> 1,154,595 iterations, 115,425 per second
>
> [ (1 to: 100) asArray sum: [ :e | e ] ifEmpty: [ 0 ] ] benchFor: 10
> seconds.
> 1,102,358 iterations, 110,203 per second
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.pharo.org/pipermail/pharo-dev_lists.pharo.org/attachments/20151222/5656ef3a/attachment-0002.html>


More information about the Pharo-dev mailing list