Chapter 8
That said, sometimes you can write faster code in C than in Perl. After careful
profiling and tuning, sometimes it turns out that 90 percent of your module's
runtime is taking place inside a small section of code. If you can replace this piece
with a call to a function written in carefully crafted C, you might realize significant
gains. The watchword here is profiling: You need to find the bottleneck and be sure
you can't optimize it using more conventional techniques.
One place where C does have a notable advantage over Perl in performance is
in memory usage. C requires you to be entirely responsible for every memory allo
cation; as a result, you gain a much higher degree of control over how your data is
stored. Sometimes this can be used to allow your programs to run in a much
smaller amount of memory than a Perl implementation would.
2
The Perl C API
To write Perl modules in C, you need to have a working knowledge of the Perl C API
(Perl API for short). This section will give you the basics, but you should know
where to go for the details: perlapi. The perlapi documentation that comes with
Perl is the Perl API bible. Nearly every C function and macro that Perl supports is
listed there along with descriptions generated from comments in the Perl source.
Data Types
For every data type available to Perl programmers, there is a corresponding C type
(see Table 8 1). Perl's data types are a lot like objects in that they each have a set of
functions that operate on them. Note that these data types are always manipu
lated through pointers.
Table 8 1. Perl Data Types
Type
Full Name
Perl Example
SV*
Scalar value
$scalar
AV*
Array value
@array
HV*
Hash value
%hash
CV*
Code value
&sub
GV*
Glob value
*glob
2. But before you decide this describes your problem, make sure you check out Bit::Vector by
Steffen Beyer and the many modules that use it to efficiently store data.
176
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved