I *think* I've found the answer to my own question...

In this post...

  https://groups.google.com/forum/#!topic/clojure/Cuk_bJrIq-Y

I found this link (I changed the line number)...

  
https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/Compiler.java#L2589

And if the implementation of eval() within the IfExpr class is anything to 
go by...

        public Object eval() {
                Object t = testExpr.eval();
                if(t != null && t != Boolean.FALSE)
                        return thenExpr.eval();
                return elseExpr.eval();
        }


Then I think Rich has implemented it so that nil takes priority over false.

...Which strengthens the case for choosing (if xs b a) over (if (nil? xs) a 
b).

Of course, in practice it probably makes no difference, as either will be 
very quick compared to other things going on.

Cheers,

Mark.

On Friday, 18 July 2014 14:39:53 UTC+9:30, Mark Phillips wrote:
>
> Thanks again - that all makes sense.
>
> One (hopefully) tiny question... an efficiency one... (and feel free not 
> to answer it if I've already taken up enough of your time)
>
> If you do that and are careful, the performance of next/nil? is slightly 
>> better than rest/empty?.
>>
>
> If I use the next/nil? idiom, is there a performance difference between 
> doing:
>
>    1. (if (nil? xs) a b)
>    2. (if xs b a)
>
> And a related question...  Clojure has two falsey values: nil and false. 
>  I assume that low-level somewhere, when checking for falsey, an (if x a b) 
> call will do something like "if x is false, short circuit to b, if x is 
> nil, short circuit to b, otherwise a".  Except, I don't know which gets 
> priority of place with the short circuits - is it false (as I've just 
> written), or is it nil?
>
> Actually, if I'm thinking about it correctly...
>
> If false takes priority then...  A branch to "a" will take three tests for 
> 1. and two tests for 2....  And a branch to "b" will take two tests for 1. 
> and two tests for 2..
>
> If nil takes priority then...  A branch to "a" will take three tests for 
> 1. and one test for 2....  And a branch to "b" will take three tests for 1. 
> and two tests for 2..
>
> So 2. is always as good or better than 1. I think - performance wise.
>
> Of course, these things are very much minor factors in overall performance 
> (especially when things like immutable data structures are involved), but I 
> wouldn't mind knowing.
>
> Cheers,
>
> Mark.
>
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to