Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
One problem with making datastructure in Perl that are a little bit more advanced than searching for exact queries, is that it's hard to parameterize the compare function. Suppose you want to create a search tree. What kind of keys are you going to store? Numbers? Strings? But to compare them, you need different operators. And when dealing with data structures, you'll do lots of comparisons.

I know of three solutions to solve this problem:

  • Use a code ref that does the compare. The drawback is that for each compare that's been made, a function call needs to be made, and that's not exactly where Perl speed shines.
  • Use a single compare function in the code. If you are going to deal with different data, store objects that overload the compare function. This even has more overhead as described above.
  • Use eval to create customized functions. Here you pay a penalty of having to do some extra compiling, but you only pay that once.

Below is a benchmark that compares the first and third options, for heapsort. It shows the eval option to be a winner.


#!/usr/bin/perl use strict; use warnings; use Benchmark; my @heap; my $M = @ARGV ? shift : 1000; sub heapify; sub heapify { my ($idx, $cmp) = splice @_ => 0, 2; my $max = $idx; for my $try (2 * $idx + 1, 2 * $idx + 2) { $max = $try if $try < @heap && $cmp -> ($heap [$try], $heap [$ +max]); } return if $max == $idx; @heap [$idx, $max] = @heap [$max, $idx]; heapify $max, $cmp; } sub extract { return unless @heap; my $cmp = shift; my $min = $heap [0]; my $tmp = pop @heap; if (@heap) { $heap [0] = $tmp; heapify 0, $cmp; } return $min; } sub heapsort_cmp (&@) { (my ($cmp), @heap) = @_; for (my $i = int (@heap / 2); $i --;) { heapify $i => $cmp; } my @result; push @result => extract $cmp while @heap; @result; } my %cache; sub heapsort_eval ($@) { (my ($cmp), @heap) = @_; unless ($cache {$cmp}) { $cache {$cmp} = 1 + keys %cache; my $sub_heapify = "heapify_$cache{$cmp}"; my $sub_extract = "extract_$cache{$cmp}"; eval <<" --"; sub $sub_heapify; sub $sub_heapify { my (\$idx) = shift; my \$max = \$idx; for my \$try (2 * \$idx + 1, 2 * \$idx + 2) { \$max = \$try if \$try < \@heap && \$heap [\$try] $cmp \$heap [\$max]; } return if \$max == \$idx; \@heap [\$idx, \$max] = \@heap [\$max, \$idx]; $sub_heapify \$max; } sub $sub_extract { return unless \@heap; my \$min = \$heap [0]; my \$tmp = pop \@heap; if (\@heap) { \$heap [0] = \$tmp; $sub_heapify 0; } return \$min; } -- if ($@) {die "eval failed: $@\n"} } no strict 'refs'; my $sub_heapify = "heapify_$cache{$cmp}"; my $sub_extract = "extract_$cache{$cmp}"; for (my $i = int (@heap / 2); $i --;) { &$sub_heapify ($i); } my @result; push @result => &$sub_extract ($cmp) while @heap; @result; } our @data = 1 .. $M; for (my $i = @data; $i --;) { my $r = rand @data; @data [$r, $i] = @data [$i, $r]; } timethese -10 => { cmp => 'heapsort_cmp {$_ [0] < $_ [1]} @::data', eval => 'heapsort_eval "<", @::data' }; __END__ Benchmark: running cmp, eval for at least 10 CPU seconds... cmp: 11 wallclock secs (10.20 usr + 0.01 sys = 10.21 CPU) @ 3.23/ +s (n=33) eval: 11 wallclock secs (10.19 usr + 0.00 sys = 10.19 CPU) @ 4.61/ +s (n=47)

In reply to Datastructures and compare functions by Abigail-II

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • 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.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2023-01-28 01:11 GMT
Find Nodes?
    Voting Booth?

    No recent polls found