[Petal] Re: Using Test::More

Fergal Daly fergal at esatclear.ie
Wed Apr 9 20:44:06 BST 2003


On Wednesday 09 April 2003 17:49, Jean-Michel Hiver wrote:
> What?
> 
> Doing something like $out .= 'something' is faster than doing push @out,
> 'something' and then joining everything? Is that what you're saying?

Yes, sorry I wan't too clear about that. try the following benchmark

use Benchmark;

my $spaces = " " x 50;
my $array = sub
{
  my @array;
  for (1..100)
  {
    push(@array, $spaces);
  }

  $string = join("", @array);
};

my $string = sub
{
  my $string = '';
  for (1..100)
  {
    $string .= $spaces;
  }
};

timethese(-5,
  {
    array => $array,
    string => $string,
  }
);


on my machine it comes out at (in iterations per second)

string: 10105.40/s
array: 3387.74/s

and surprisingly enough, if you comment out the join at the end of the array 
it makes very little difference (3750.95/s).

Even changing the 50 to 5000 strings come out about 2 times faster than 
arrays.

It seems that push is a far more expensive operation than .= .

> > The main difference is that I don't use the Petal hash, I just use an 
ordinary 
> > hash. I parse all the expressions beforehand so compile time is longer but 
> > then execution is speeded up a lot.
> 
> How do you know wether 'foo/bar' is $foo->{bar} or $foo->bar() ?

If you use the alternative syntax below then it doesn't arise but if you use 
standard Petales it gets translated into something like

do {
	my $thing = $hash->{foo};
	if (UNIVERSAL::can($thing, "bar")
	{
		$thing->bar();
	}
	elsif( ref($thing) eq 'HASH' or $thing =~ /=HASH\(/ )
	{
		$thing->{"bar"};
	}
	else
	{
		die "Can't use 'bar' on $thing";
	}
}

As you can see it doesn't test for an ARRAY ref because the key was not 
numeric. If the key was numeric then it would replace the object test with an 
array test.

Unfortunately it's messy having these directly in the source code. It could be 
replaced by a call to a subroutine instead but that would have some extra 
overhead. It's a pity Perl can't inline subroutines, then it'd be the best of 
both worlds.

> > a/hash_key/5/get()
> 
> I don't think that the '()' part is TALES compliant, but I could be
> wrong.

You're right, that was a typo, the () is part of the modified syntax.

> > advantage of this, it almost doubles in speed again but it's a big break 
in 
> > compatibilty with TALES.
> 
> Plus it looks really bad I think.

Complain to Larry Wall about that one ;-) it's based on Perl.

It's an extension that uses characters that aren't reserved for any other 
purpose so people can ignore it if they want. I suppose, depending on the 
template the benefits can be anywhere from very large to very small.

> Anyway, thanks for your to-come improvements! I'm sure everybody on the
> list appreciates that.

Luckily enough your code is very easy to follow and Petal was built for easy 
extension,

F




More information about the Petal mailing list