Short about 'sort'

Discussion of Common Lisp

Re: Short about 'sort'

Postby 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*
           for i from 0 below length
           collect (cons (random 1000000) 'x)))

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

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

Code: Select all
This is SBCL 1.0.22, an implementation of ANSI Common Lisp.
More information about SBCL is available at <>.

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")

* (init-big-list 100000)

* (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

* (init-big-list 100000)

* (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


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.
Posts: 440
Joined: Sat Jun 28, 2008 7:49 am
Location: Austin, TX


Return to Common Lisp

Who is online

Users browsing this forum: No registered users and 6 guests