[Rd] graphic for the R profiler

From: Romain Francois <rfrancois_at_mango-solutions.com>
Date: Sat, 30 Jun 2007 01:12:50 +0100


Hello all,

I just wanted to share a small perl script that generates a dot file from the result of the R profiler. The dot file can than be used to create a graphical display of the profiling. You can save this file in the bin directory of your R installation and then create a graph, for example an SVG by piping the output of the script to dot:

$ R CMD Rprof2dot Rprof.out | dot -Tsvg > test3.svg

Some example graphics are presented in the R wiki here: http://wiki.r-project.org/rwiki/doku.php?id=tips:misc:profiling

Cheers,

Romain

<code perl>

#! /usr/bin/perl  

use Getopt::Long;  

my $cutoff=5;
GetOptions ('cutoff=s' => \$cutoff );  

%calltree = ();
%allfun = ();  

while (<>) {
  if (/^sample\.interval=/) {

  	s <http://www.perldoc.com/perl5.6/pod/func/s.html>/sample\.interval=//;
  	$sample = $_ / 1e6;
  } else {
	  chomp <http://www.perldoc.com/perl5.6/pod/func/chomp.html>;
	  @line = reverse <http://www.perldoc.com/perl5.6/pod/func/reverse.html> split <http://www.perldoc.com/perl5.6/pod/func/split.html>(/ /);
	  

$caller = shift <http://www.perldoc.com/perl5.6/pod/func/shift.html>(@line);
$allfun{$caller}++;

    while( $called = shift <http://www.perldoc.com/perl5.6/pod/func/shift.html>(@line) ){

      $allfun{$called}++;
      $calltree{$caller}{$called} ++ ;
      $caller = $called;

    }     

  }
}  

print <http://www.perldoc.com/perl5.6/pod/func/print.html> "digraph {\n" ; print <http://www.perldoc.com/perl5.6/pod/func/print.html> 'graph [ rankdir = "LR"]; '."\n"; foreach $fun (keys <http://www.perldoc.com/perl5.6/pod/func/keys.html> %allfun){   $_ = $fun;
  s <http://www.perldoc.com/perl5.6/pod/func/s.html>/"$//;   $value = $allfun{$fun} ;
  print "$fun [shape=rect,fontsize=6,label=$_\\n(".$value.")\"] \n" if $value > ($cutoff-1) ; }   

foreach $caller (keys <http://www.perldoc.com/perl5.6/pod/func/keys.html> %calltree){   for $called ( keys <http://www.perldoc.com/perl5.6/pod/func/keys.html> %{ $calltree{$caller} } ) {
$value = $calltree{$caller}{$called} ;
$n1 = $allfun{$called} ;
$n2 = $allfun{$caller} ;
    

    print <http://www.perldoc.com/perl5.6/pod/func/print.html> " $caller -> $called [label=" . $value. ",fontsize=6]\n" if ( $value > $cutoff );

  }
}
print <http://www.perldoc.com/perl5.6/pod/func/print.html> "}\n

</code>

-- 
Mango Solutions
data analysis that delivers

Tel:  +44(0) 1249 467 467
Fax:  +44(0) 1249 467 468
Mob:  +44(0) 7813 526 123

______________________________________________
R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Sat 30 Jun 2007 - 00:33:53 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 30 Jun 2007 - 12:35:16 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.