... says ...#!/usr/bin/perl use strict; use warnings; use Benchmark qw(cmpthese); cmpthese(-1, { bless => sub { my @o; for (0..1000) { my $h = { foo => 'bar' }; bless $h, 'Foo'; push @o, $h; } }, nobless => sub { my @o; for (0..1000) { my $h = { foo => 'bar' }; push @o, $h; } }, bless_scalar => sub { my @o; for (0..1000) { my $s = \(my $t); bless $s, 'Foo'; push @o, $s; } } } );
so, the bless operation is pretty cheap.Rate bless nobless bless_reuse bless 575/s -- -32% -39% nobless 845/s 47% -- -11% bless_scalar 948/s 65% 12% --
On the other hand, method calls are quite more expensive that accesing a hash entry.
In reply to Re: Time efficiency of object creation in Perl (or the "bless" function)
by salva
in thread Time efficiency of object creation in Perl (or the "bless" function)
by kikumbob
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |