From rfrancois at mango-solutions.com  Sat Jun 30 02:12:50 2007
From: rfrancois at mango-solutions.com (Romain Francois)
Date: Sat, 30 Jun 2007 01:12:50 +0100
Subject: [Rd] graphic for the R profiler
Message-ID: <4685A002.7010002@mango-solutions.com>

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



[Rd] graphic for the R profiler
Romain Francois