1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
#!/usr/bin/perl -w
use Benchmark;
use Graph::Easy;
use Time::HiRes qw/time/;
use strict;
use Devel::Size qw/total_size/;
print "# Graph::Easy v", $Graph::Easy::VERSION,"\n";
print "Creating graph...\n";
my ($g,$n,$last);
time_it ( \&create, shift);
print "Creating txt...\n";
time_it ( \&as_txt );
# dump the text for later
#print STDERR $g->as_txt(); exit;
#print STDERR $g->as_graphviz(); exit;
# $g->timeout(20) if $g->can('timeout');
print $g->as_ascii() if $g->nodes() < 40;
# for profile with -d:DProf
#for (0..5) { $g->layout(); } exit;
print "\n";
exit if shift;
print "Benchmarking...\n";
$n = $g->node('1');
timethese (-5,
{
'node cnt' => sub { scalar $g->nodes(); },
'edge cnt' => sub { scalar $g->edges(); },
'nodes' => sub { my @O = $g->nodes(); },
'edges' => sub { my @O = $g->edges(); },
"conn's" => sub { $n->connections(); },
"succ's" => sub { scalar $n->successors(); },
"succ' cnt" => sub { my @O = $n->successors(); },
"edges_to" => sub { my @O = $n->edges_to($last) },
# "layout" => sub { $g->layout(); },
# "as_txt" => sub { $g->as_txt(); },
} );
sub time_it
{
my $time = time;
my $r = shift;
&$r(@_);
printf ("Took %0.4fs\n", time - $time);
}
sub as_txt
{
my $t = $g->as_txt();
}
sub create
{
my $cnt = abs(shift || 1000);
$g = Graph::Easy->new();
$n = Graph::Easy::Node->new('0');
$last = Graph::Easy::Node->new('1');
for (2..$cnt)
{
my $node = Graph::Easy::Node->new($_);
$g->add_edge($last, $node);
my $n2 = Graph::Easy::Node->new($_.'A');
$g->add_edge($last, $n2);
my $n3 = Graph::Easy::Node->new($_.'B');
$g->add_edge($last, $n3);
$last = $node;
}
# prior to 0.25, the two calls to nodes() and edges() will take O(N) time, further
# slowing down this routine by about 10-20%.
print "Have now ", scalar $g->nodes(), " nodes and ", scalar $g->edges()," edges.\n";
print "Graph objects takes ", total_size($g), " bytes.\n";
}
|