The remaining difference in memory is due to the Perl version knowing exactly how big the array will be from the start (because the whole list is assigned in one go):
FILL = 999
MAX = 999
The C version causes the arrays to grow and leaves space for growth:
FILL = 999
MAX = 1021
By pre-extending the arrays,
SV *make_aoa_c( int n_rows, int n_cols ) {
int i, j;
char *foo = "foo";
AV *table = newAV();
av_extend(table, n_rows-1); /* <---------- */
for ( i = 0; i < n_rows; ++i ) {
AV *row = newAV();
av_extend(row, n_cols-1); /* <---------- */
for ( j = 0; j < n_cols; ++j ) {
av_push( row, newSVpv( foo, 0 ) );
}
av_push( table, newRV_noinc( row ) );
}
return newRV_noinc( table );
}
both the Perl and the C data structures are identical.
FILL = 999
MAX = 999
and the process that calls the C version uses less memory (perhaps from reduced stack usage?)
$ perl test_aoa.pl
1: 78688 (184871 us)
2: 78696 (298376 us)
3: 78696 (196999 us)
4: 78696 (204391 us)
5: 78696 (225786 us)
$ perl test_aoa.pl use_xs
1: 78604 (321481 us)
2: 78616 (360377 us)
3: 78616 (219468 us)
4: 78616 (211587 us)
5: 78616 (209231 us)
The times are comparable, but note this it a busy machine.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.