XML:RAI XML::RSSとの比較
XML::RAIというRSS Parser
モジュールがあります。
The RSS Abstraction Interface, or RAI (said "ray"), provides an object-oriented interface to XML::RSS::Parser trees that abstracts the user from handling namespaces, overlapping and alternate tag mappings.
どういうものかというと、
my $rai = XML::RAI->parse($doc);
foreach my $item (@{$rai->items}){
print $item->content;
}
としたときに表示されるのは、
* xhtml:body
* xhtml:div
* content:encoded
* description
* dc:description
* rss091:description
の中から自動的に選択してくれます。
RSSにはいろいろバージョンありますけど、その違いも吸収できるのです。
RSSのパーサーとしては、XML::RSSがスタンダートなのだが、比較のベンチマークをしてみた。
結果1 - このblogのRSS(v1.0)を読み込ませた場合
Benchmark: timing 100 iterations of XML::RAI, XML::RSS... XML::RAI: 23 wallclock secs (23.20 usr + 0.11 sys = 23.31 CPU) @ 4.29/s (n=100) XML::RSS: 33 wallclock secs (33.36 usr + 0.01 sys = 33.37 CPU) @ 3.00/s (n=100)
これを見て、XML::RSSより速いのかと、思ったが、
結果2 - Yahoo!の新着情報のRSS(v2.0)の場合
Benchmark: timing 100 iterations of XML::RAI, XML::RSS... XML::RAI: 8 wallclock secs ( 8.10 usr + 0.02 sys = 8.12 CPU) @ 12.32/s (n=100) XML::RSS: 6 wallclock secs ( 5.15 usr + 0.00 sys = 5.15 CPU) @ 19.42/s (n=100)
という結果が出るところをみると、データによりそれぞれ。
まぁ、両方とも、XML::Parserを使っているのでそれほど違いがでないのでしょう。
前回のベンチで試したXML::LibXMLが速いのは間違いない。
ただ、XML::RAIは便利だと思う。
以下実行したスクリプト
#!/usr/bin/perl -w
use strict;
use IO::File;
use XML::RAI;
use XML::RSS;
use Benchmark;
my $fh = IO::File->new($ARGV[0]) or die $!;
my $doc="";
while(my $ret=$fh->getline){
$doc.=$ret;
}
Benchmark::timethese(100, {
'XML::RSS' =>\&with_xml_rss,
'XML::RAI' => \&with_xml_rai,
});
sub with_xml_rss{
my %rss;
my $rss = XML::RSS->new;
$rss->add_module(
prefix=>"content",
uri=>"http://purl.org/rss/1.0/modules/content/"
);
$rss->parse($doc);
my $channel = $rss->channel;
$rss{title} = $channel->{title};
$rss{link} = $channel->{link};
my @items;
foreach my $item (@{$rss->{items}}){
my %item;
$item{title}=$item->{title};
$item{link}=$item->{link};
$item{description}=$item->{content}->{encoded} || $item->{description};
$item{date}=$item->{dc}->{date};
push(@items,\%item);
}
$rss{items}=\@items;
}
sub with_xml_rai{
my %rss;
my $rai = XML::RAI->parse($doc);
my $channel=$rai->channel;
$rss{title}=$channel->title;
$rss{link}=$channel->link;
my @items;
foreach my $item (@{$rai->items}){
my %item;
$item{title}=$item->title;
$item{link}=$item->link;
$item{description}=$item->content;
$item{date}=$item->issued;
}
$rss{items}=\@items;
}
コメント
初めまして。
RAIのベンチですが、
pushしてませんよー
投稿者: ta2u | 2005年05月25日 16:01