On Tue, 20 Oct 2009 23:47:29 -0400, Daniel Herring wrote:
> Hi all,
> Consider these trivial test cases
>
> (defun test1 (x y)
> (declare (type fixnum x y))
> (setf (values x y)
> (values y x))
> (list x y))
>
> (defun test2 (x y)
> (declare (type fixnum x y))
> (let ((tmp y))
> (setf x y
> y tmp))
> (list x y))
>
> Stylistically, test1 represents the permutation more cleanly than test2;
> but test2 appears to compile more efficiently.
For me, with (declare (optimize (speed 3))) in both functions, test1
and test2 are about the same (maybe a difference of 3-4 instructions),
so I don't see why one is more "efficient" than the other. Also, when
comparing efficiency, do benchmarking/profiling instead of counting
instructions: whole sections of code could be skipped unless there is
an exception, so using line counting to compare for speed is unwise.
But I guess if you tried benchmarking these, the difference would be
lost in the noise.
Do you care about this example? Is this the bottleneck in your code?
Or are you just fooling around with SBCL to see how it optimizes? Be
advised that you will learn very little from examples like this.
I had my assumptions on what is fast and what is not when I started
using SBCL, but soon learned that some things were optimized exactly
because they were thought to be slow. So these days I don't try to
second-guess the compiler, just optimize bottlenecks.
> Is there another fast, elegant way to effect permutations that I'm
> missing? Would it be hard to teach SBCL to better optimize test1? Did
> I miss the proper optimization magic? Do I just need to upgrade?
(defun test3 (x y)
(list y x))
should be semantically equivalent to your function. If concerned
about speed, inline it.
For real-world code, see rotatef.
HTH,
Tamas