How to make
Lisp
go faster than C
Didier Verna
∗
Abstract
Contrary to popular belief,
Lisp
code can be very ef-
cient today: it can run as fast as equivalent C code
or even faster in some cases. In this paper, we explain
how to tune
Lisp
code for performance by introducing
the proper type declarations, using the appropriate
data structures and compiler information. We also
explain how eciency is achieved by the compilers.
These techniques are applied to simple image process-
ing algorithms in order to demonstrate the announced
performance on pixel access and arithmetic operations
in both languages.
Keywords: Lisp, C, Numerical Calculus, Image Pro-
cessing, Performance
1 Introduction
More than 15 years after the standardization process
of
Common-Lisp
[5], and more than 10 years after
people really started to care about performance [1, 4],
Lisp
still suers from the legend that it is a slow lan-
guage. Although perfectly justied back in the 60's,
this idea has become obsolete long ago: today,
Lisp
can run as fast, or even
faster
than C.
If this false idea of slowness is still widespread today,
it is probably because of the lack of knowledge, or
misunderstanding of the 4 key factors for getting per-
formance in
Lisp
:
Compilers
While
Lisp
is mainly known to be an in-
terpreted language, there are very good and e-
cient compilers out there, that can deliver not
only byte-code, but also
native machine code
from
Lisp
source.
Static Typing
While
Lisp
is mainly known for its
dynamically typed nature, the
Common-Lisp
standard provides means to declare variable types
∗
Epita
Research and Development Laboratory, 1416 rue
Voltaire, F-94276 Le Kremlin-Bicêtre, France. Email: di-
dier@lrde.epita.fr
statically (hence known at compile-time), just as
you would do in C.
Safety Levels
While dynamically typed
Lisp
code
leads to dynamic type checking at run-time, it
is possible to instruct the compilers to bypass
all safety checks in order to get optimum per-
formance.
Data Structures
While
Lisp
is mainly known for
its basement on list processing, the
Common-
Lisp
standard features very ecient data types
such as specialized arrays, structs or hash tables,
making lists almost completely obsolete.
Experiments on the performance of
Lisp
and C were
conducted in the eld of image processing. We bench-
marked, in both languages, 4 simple algorithms to
measure the performances of the fundamental low-
level operations: massive pixel access and arithmetic
processing. As a result, we got at least equivalent
performance from C and
Lisp
, and even a 10% im-
provement with
Lisp
code in some cases.
In this paper, we present the algorithms used for
benchmarking, and the experimental performance re-
sults we obtained. We also demonstrate how to prop-
erly tune the corresponding
Lisp
code by introducing
the proper type declarations, using the appropriate
data structures and compiler information, in order to
get the announced performance.
2 Experimental Conditions
2.1 The Algorithms
Our experiments are based on 4 very simple algo-
rithms: pixel assignment (initializing an image with a
constant value), and pixel addition / multiplication /
division (lling a destination image with the addition
/ multiplication / division of every pixel from a source
image by a constant value). These algorithms, while
very simple, are pertinent because they involve the
fundamental atomic operations of image processing:
pixel access and arithmetic processing.
IAENG International Journal of Computer Science, 32:4, IJCS_32_4_19
______________________________________________________________________________________
(Advance online publication: 12 November 2006)