first commit

This commit is contained in:
douboer
2025-09-17 16:08:16 +08:00
parent 9395faa6b2
commit 3ff47c11d5
1318 changed files with 117477 additions and 0 deletions

View File

@@ -0,0 +1,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";
}

View File

@@ -0,0 +1,124 @@
#!/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";
my @results;
my ($n,$last,$g, $size);
my @counts = ( qw/5 10 50 100 200 500 1000/ );
for my $count (@counts)
{
print "Creating graph with ", $count * 3, " nodes and edges...\n";
my $rc = [ ];
push @$rc, time_it ( \&create, $count);
$size = total_size($g);
print "Graph objects takes $size bytes.\n";
print "Creating txt...\n";
print $g->as_ascii() if $count == 5;
if ($Graph::Easy::VERSION < 0.25 && ($count > 500))
{
print "Skipping as_foo() tests.\n";
push @$rc, 0, 0;
}
else
{
push @$rc,
time_it ( \&as_txt ),
time_it ( \&as_ascii);
}
push @$rc, $size;
push @results, $rc;
}
print "Results\n";
for my $r (@results)
{
print join (" ", @$r),"\n";
}
print " <tr>\n <th>Graph::Easy v$Graph::Easy::VERSION</th>\n <th>"
. join ("</th>\n <th>", @counts) . "</th>\n </tr>\n";
my $i = 0;
for my $t ( qw/Creation as_txt as_ascii Memory/ )
{
print " <tr>\n <td>$t</td>\n";
for my $r (@results)
{
print " <td>$r->[$i]</td>\n";
}
print " </tr>\n";
$i++;
}
#print STDERR $g->as_graphviz();
1;
#############################################################################
sub time_it
{
my $time = time;
my $r = shift;
&$r(@_);
my $took = sprintf ("%0.4f", time - $time);
print "Took ${took}s\n";
$took;
}
sub as_txt
{
my $t = $g->as_txt();
}
sub as_ascii
{
my $t = $g->as_ascii();
}
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+1)
{
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";
$g->{timeout} = 120;
}

View File

@@ -0,0 +1,137 @@
#!/usr/bin/perl -w
BEGIN
{
use lib 'lib';
$|++;
}
use Scalar::Util qw/weaken/;
use Time::HiRes qw/time/;
use Data::Dumper;
use Graph::Easy;
my $N1 = shift || 5000;
my $N2 = shift || 40000;
my $STEP = shift || 2;
# results
my $RC = [];
print "Using Graph::Easy v$Graph::Easy::VERSION\n";
for (my $N = $N1; $N < $N2; $N *= $STEP)
{
my @R = ($N);
my $start = time();
print scalar localtime(), " start\n";
normal($N);
print scalar localtime(), " done, took ", sprintf("%.2f", time() - $start)," seconds\n";
push @R, sprintf("%.2f",time() - $start);
$start = time();
print scalar localtime(), " start\n";
my $graph = graph($N); # return the graph to show that creation sep.
print scalar localtime(), " done creation, took ", sprintf("%.2f", time() - $start)," seconds\n";
push @R, sprintf("%.2f",time() - $start);
$start = time();
$graph = undef;
print scalar localtime(), " done destroy, took ", sprintf("%.2f", time() - $start)," seconds\n";
push @R, sprintf("%.2f",time() - $start);
$start = time();
push @$RC, [ @R ];
}
print "\n";
print "\n", join("\t\t", 'N', 'Normal', 'Graph-Easy'), "\tGraph-Easy\n";
print join("\t\t", '', '', 'Create','Destroy'), "\n";
print '-' x 70,"\n";
# print results
for my $R (@$RC)
{
print join("\t\t", @$R), "\n";
}
sub graph
{
my $N = shift;
my $graph = Graph::Easy->new();
# create N objects, and "link" them together
for my $i (1..$N)
{
my $b = $i; $b++;
$graph->add_edge($i,$b);
}
print Dumper($graph),"\n" if $N < 10;
$graph;
}
sub normal
{
my $N = shift;
my $container = {};
my $old_object;
# create N objects, and "link" them together
for my $i (1..$N)
{
my $o = new_object($i);
$container->{nodes}->{$i} = $o;
$o->{graph} = $container;
weaken($o->{graph});
if ($old_object)
{
my $link = new_link($old_object, $o, $i);
$container->{edges}->{$i} = $link;
$link->{graph} = $container;
{
no warnings;
weaken($link->{graph});
weaken($link->{to}->{graph});
weaken($link->{from}->{graph});
}
}
$old_object = $o;
}
print Dumper($container),"\n" if $N < 10;
}
sub new_object
{
my $id = shift;
my $o = bless { id => $id, att => {}, }, 'main';
$o;
}
sub new_link
{
my ($a,$b,$id) = @_;
my $link = bless { id => $id, from => $a, to => $b, att => {} }, 'main';
$a->{edges}->{$id} = $link;
$b->{edges}->{$id} = $link;
$link;
}

View File

@@ -0,0 +1,18 @@
digraph GRAPH_0 {
// Generated by Graph::Easy 0.38 at Sat Dec 31 16:13:04 2005
edge [ arrowhead=open ];
graph [ rankdir=LR ];
node [
fontsize=11,
fillcolor=white,
style=filled,
shape=box ];
Berlin [ URL="/wiki/index.php/Berlin" ]
Bonn [ URL="/wiki/index.php/Bonn" ]
Bonn -> Berlin
}

View File

@@ -0,0 +1,2 @@
graph { autolink: name; }
[ Bonn ] -> [ Berlin ]