Just tried this very simple one: (I guess the smaller the merrier)
floorLog10bert "from Bert Freundenberg's idea"
(self negative or: [self isZero]) ifTrue: [^self error: 'Can''t take log of number <= 0']. self < 10 ifTrue: [^0]. self < 100 ifTrue: [^1]. self < 1000 ifTrue: [^2]. self < 10000 ifTrue: [^3]. self < 100000 ifTrue: [^4]. self < 1000000 ifTrue: [^5]. self < 10000000 ifTrue: [^6]. self < 100000000 ifTrue: [^7]. self < 1000000000 ifTrue: [^8]. ^9 "Based on the fact that (SmallInteger maxVal log: 10) floor = 9"
Comparing universal Integer version provided in previous mail and Bert's gives a factor 5 at least
Time millisecondsToRun: [1000000 timesRepeat: [SmallInteger maxVal floorLog10 ]]. 19846 Time millisecondsToRun: [1000000 timesRepeat: [SmallInteger maxVal floorLog10bert ]]. 3790 3777
Ain't that much to gain when reducing number of tests anyway: Time millisecondsToRun: [1000000 timesRepeat: [1 floorLog10bert ]]. 2563
Seems fast enough not to bother.
Nicolas
Bert Freudenberg a écrit :
The nested conditionals might still win ... in particular if it was not binary search but based on frequency of numbers.
- Bert -