Integer>even Fast but Integer>odd Slow?

VA Smalltalk is a "100% VisualAge compatible" IDE that includes the original VisualAge technology and the popular VA Assist and WidgetKit add-ons.

Moderators: Eric Clayberg, wembley, tc, Diane Engles, solveig

Integer>even Fast but Integer>odd Slow?

Postby pepe4321 » Tue Jul 21, 2009 7:47 am

Hi.
Since Vast 6.0.4 the implementation of Integer>>even changed from this:
Integer>>even

"Answer a Boolean, true if the
receiver is an even number;
answer false otherwise. Even means divisible
by 2 with no remainder."

^(self rem: 2 ) = 0

to this:
Integer>>even

"Answer a Boolean true if the receiver is an even number;
answer false otherwise. Even means divisible by 2 with
no remainder."

^(self bitAnd: 1) ~= 0

But not the implementation of Integer>>odd, which is this:
Integer>>odd

"Answer a Boolean indicating true if
the receiver is an odd number; answer
false otherwise. Odd is defined as
divisable by 2 with a non-zero remainder."

^(self rem: 2 ) ~= 0

Is there any reason for it?

Thanks,
JP
pepe4321
 
Posts: 2
Joined: Tue Jul 21, 2009 7:38 am

Re: Integer>even Fast but Integer>odd Slow?

Postby wembley » Wed Jul 22, 2009 7:20 am

I suppose it was an oversight by whoever made Integer>>#even run faster -- let's see, that was me :oops:

Anyway, a similar change will be made to Integer>>#odd.

Case 42456 opened -- fixed in V8.0.1.
John O'Keefe [|], Principal Smalltalk Architect, Instantiations Inc.
wembley
Moderator
 
Posts: 405
Joined: Mon Oct 16, 2006 3:01 am
Location: Durham, NC

Re: Integer>even Fast but Integer>odd Slow?

Postby koschate » Wed Jul 22, 2009 4:26 pm

Maybe it's naive of me, but I would have expected #odd to have been implemented as

odd
^self even not

both for obviousness and DRY principles. In terms of performance, I don't think there's a hit, and this would have avoided the problem.
koschate
[|]
 
Posts: 102
Joined: Thu Feb 01, 2007 7:24 am

Re: Integer>even Fast but Integer>odd Slow?

Postby wembley » Fri Aug 14, 2009 8:30 am

koschate wrote:Maybe it's naive of me, but I would have expected #odd to have been implemented as

odd
^self even not

both for obviousness and DRY principles. In terms of performance, I don't think there's a hit, and this would have avoided the problem.


It actually makes a big difference:

100,000,000 loops of self even not takes 3.930 seconds on my VMware Ubuntu machine

100,000,000 loops of (self bitAnd: 1) == 1 takes 2.784 seconds on the same machine

In very small methods, one extra message send can make a big difference -- I suppose that is why the methods weren't implemented in terms of themselves in the first place.
John O'Keefe [|], Principal Smalltalk Architect, Instantiations Inc.
wembley
Moderator
 
Posts: 405
Joined: Mon Oct 16, 2006 3:01 am
Location: Durham, NC


Return to VA Smalltalk 7.0, 7.5 & 8.0

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest