On Mon, 24 May 2010, Lukas Renggli wrote:
For now the list of levente and a slow machine
"So "== nil" #ifNil:, #ifNil:ifNotNil:, #ifNotNil:, #ifNotNil:ifNil: should
be used whenever possible."
is the one to use
Please read my comments on the issue tracker.
For *any practical* example it doesn't matter what to use speed wise:
That's not true. Most of the time it doesn't matter, but when these
checks/conditional statements are in tight loops they can make a huge
difference. Core packages have some good code examples and those have to
be fast, but not cross-dialect. I'm sure other smalltalks are implemented
this way.
1 = nil ifTrue: [ ]
2 == nil ifTrue: [ ]
3 isNil ifTrue: [ ]
4 ifNil: [ ]
IMHO 4 is the most readable and it is the fastest in Pharo/Squeak at the
moment, so that should be preferred in core packages.
2 is cross-dialect and it's probably the fastest in all dialects, so if I
were writing cross-dialect code then I'd probably use that.
3 looks nice and #isNil is inlined in some dialects (AFAIK), but it's
"slow" in Squeak/Pharo.
1 just looks bad, I can't imagine an OODB that doesn't map it's "null
object" directly to nil, otherwise #= will be asymmetric:
null = nil -> true
nil = null -> false
Btw I hope we will have an inlining JIT soon which can decrease/eliminate
these performance differences.
Levente
Personally I find 3 the most readable. 1 and 3 are the only practical
ones if you use OODBs, because they are not shortcut by the compiler.
I only use 4 for lazy initializers, it makes a good pattern. The use
of #ifNil:ifNotNil:, #ifNotNil:, #ifNotNil:ifNil: is only practicable
if you don't care about compatibility with other Smalltalk dialects.
Lukas
--
Lukas Renggli
www.lukas-renggli.ch
_______________________________________________
Pharo-project mailing list
Pharo-project@lists.gforge.inria.fr
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
_______________________________________________
Pharo-project mailing list
Pharo-project@lists.gforge.inria.fr
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project