ovedpo15 has asked for the wisdom of the Perl Monks concerning the following question:
Once I fill the graph, I want to have a sub that returns all the possible paths, including links. So for example, assume you have /a/b/c again and /p->/a/b then I want it to return /a,/a/b,/a/b/c,/p,/p/c.my $graph = Graph->new; $graph->set_vertex_attributes("/", { "type" => "dir" }); my $self = { graph => $graph };
My code was very slow and I managed to see that it comes from this method. I used NYTProf to create a profiling report and you can find it here. It looks like the problem is with the following line which takes to much time:sub extract_paths_from_graph { my ($self,$paths_href) = @_; foreach my $vertex ($self->{"graph"}->unique_vertices) { $paths_href->{$vertex}++; } while (1) { my $found_changes = 0; foreach my $vertex ($self->{"graph"}->unique_vertices) { my $type = $self->{"graph"}->get_vertex_attribute($vertex, + 'type'); if (index($type,"link") != -1) { # Ignore cycle in graph to prevent infinite loop my $target = ($self->{"graph"}->successors($vertex))[0 +]; if (path($target)->subsumes($vertex)) { next; } foreach my $subpath (keys(%{$paths_href})) { if ($subpath =~ /^$target\// || $subpath =~ /^$tar +get$/) { my $new_vertex = $subpath =~ s/^$target/$verte +x/gr; $found_changes = 1 unless (defined($paths_href +->{$new_vertex})); $paths_href->{$new_vertex}++; } } } } last unless ($found_changes); } }
My assumptions are correct? Can you please suggest a better alternative way to perform the check? I though regex here will be the fastest.if ($subpath =~ /^$target\// || $subpath =~ /^$target$/) {
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: experiencing slowness due to matching algorithm
by hippo (Archbishop) on Jan 09, 2023 at 09:53 UTC | |
by ovedpo15 (Pilgrim) on Jan 09, 2023 at 10:36 UTC | |
by hippo (Archbishop) on Jan 09, 2023 at 11:28 UTC | |
|
Re: experiencing slowness due to matching algorithm
by NERDVANA (Priest) on Jan 09, 2023 at 15:57 UTC | |
|
Re: experiencing slowness due to matching algorithm
by bliako (Abbot) on Jan 10, 2023 at 08:35 UTC |