<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!perl -w
use strict;
use Benchmark qw(:hireswallclock countit);
use Imager;

my $im = Imager-&gt;new(xsize =&gt; 1000, ysize =&gt; 1000);
my $ima = $im-&gt;convert(preset =&gt; "addalpha");
my $im_pal = Imager-&gt;new(xsize =&gt; 1000, ysize =&gt; 1000, type =&gt; "paletted");
my @colors = map Imager::Color-&gt;new($_), qw/red green blue white black/;
$im_pal-&gt;addcolors(colors =&gt; \@colors);
my $color = $colors[0];
my $other = Imager::Color-&gt;new("pink");

countthese
  (5,
   {
    gray =&gt; sub {
      my $temp = $im-&gt;convert(preset =&gt; "grey")
	for 1 .. 10;
    },
    green =&gt; sub {
      my $temp = $im-&gt;convert(preset =&gt; "green")
	for 1 .. 10;
    },
    addalpha =&gt; sub {
      my $temp = $im-&gt;convert(preset =&gt; "addalpha")
	for 1 .. 10;
    },
    noalpha =&gt; sub {
      my $temp = $ima-&gt;convert(preset =&gt; "noalpha")
	for 1 .. 10;
    },
   }
  );

$im_pal-&gt;type eq "paletted" or die "Not paletted anymore";

sub countthese {
  my ($limit, $what) = @_;

  for my $key (sort keys %$what) {
    my $bench = countit($limit, $what-&gt;{$key});
    printf "$key: %.1f /s (%f / iter)\n", $bench-&gt;iters / $bench-&gt;cpu_p,
      $bench-&gt;cpu_p / $bench-&gt;iters;
  }
}

__END__

Original:

addalpha: 1.3 /s (0.797143 / iter)
gray: 4.3 /s (0.233636 / iter)
green: 4.9 /s (0.205600 / iter)
noalpha: 1.6 /s (0.608889 / iter)

convert_via_copy:

addalpha: 4.9 /s (0.205600 / iter)
gray: 4.2 /s (0.235909 / iter)
green: 8.6 /s (0.115682 / iter)
noalpha: 5.4 /s (0.185556 / iter)

</pre></body></html>