Short about 'sort'

Discussion of Common Lisp
findinglisp
Posts: 447
Joined: Sat Jun 28, 2008 7:49 am
Location: Austin, TX
Contact:

Re: Short about 'sort'

Post by findinglisp » Thu Feb 26, 2009 11:47 am

Just to convince myself...

Code: Select all

(in-package :cl-user)

(defvar *big-list*)

(defun init-big-list (length)
  (setf *big-list*
        (loop
           for i from 0 below length
           collect (cons (random 1000000) 'x)))
  nil)

(defun sort1 ()
  (setf *big-list* (sort *big-list* #'< :key #'car))
  nil)

(defun sort2 ()
  (setf *big-list* (sort *big-list* #'(lambda (x y) (< (car x) (car y)))))
  nil)

Code: Select all

This is SBCL 1.0.22, an implementation of ANSI Common Lisp.
More information about SBCL is available at <http://www.sbcl.org/>.

SBCL is free software, provided as is, with absolutely no warranty.
It is mostly in the public domain; some portions are provided under
BSD-style licenses.  See the CREDITS and COPYING files in the
distribution for more information.
* (load "temp.lisp")

T
* (init-big-list 100000)

NIL
* (time (sort1))

Evaluation took:
  0.255 seconds of real time
  0.247963 seconds of total run time (0.247963 user, 0.000000 system)
  97.25% CPU
  465,859,427 processor cycles
  0 bytes consed

NIL
* (init-big-list 100000)

NIL
* (time (sort2))

Evaluation took:
  0.230 seconds of real time
  0.219966 seconds of total run time (0.214967 user, 0.004999 system)
  95.65% CPU
  420,188,509 processor cycles
  0 bytes consed

NIL
*
So, the version with everything in a single LAMBDA is slightly faster, as predicted. This is consistent across multiple invocations with SBCL.
Cheers, Dave
Slowly but surely the world is finding Lisp. http://www.findinglisp.com/blog/

Post Reply