The most upvoted stackoverflow question - also holds for Pharo

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

The most upvoted stackoverflow question - also holds for Pharo

Kasper Osterbye

I wanted to see if the results could be reproduced in Pharo, and indeed they can (though I only get a factor 2-3 difference). The following method does the trick:

branchPrediction
| size data rnd sum start |
size := 32768.
data := ByteArray new: size.
rnd := Random new: 0.
1 to: size do: [ :i | data at: i put: (rnd nextInt: 256) - 1 ].
“sorting the array makes the loop faster"
data sort.
sum := 0.
start := DateAndTime current.
10000
timesRepeat: [ 1 to: size do: [ :i | 
(data at: i) > 128
ifTrue: [ sum := sum + (data at: i) ] ] ].
^ {(DateAndTime current - start) asMilliSeconds.
sum}

Though it is old news, it is still kind of cool.

Best,

Kasper