From rfrancois at mango-solutions.com  Sat Jun 30 08:07:44 2007
From: rfrancois at mango-solutions.com (Romain Francois)
Date: Sat, 30 Jun 2007 07:07:44 +0100
Subject: [Rd] graphic for the R profiler
Message-ID: <4685F330.7090403@mango-solutions.com>

((resent without the generated links, thanks Dirk))

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/sample\.interval=//;
   	$sample = $_ / 1e6;
   } else {
	  chomp;
	  @line = reverse split(/ /);
	
     $caller = shift(@line);
     $allfun{$caller}++;
     while( $called = shift(@line) ){
       $allfun{$called}++;
       $calltree{$caller}{$called} ++ ;
       $caller = $called;
     }

   }
}

print "digraph {\n" ;
print 'graph [ rankdir = "LR"]; '."\n";
foreach $fun (keys %allfun){
   $_ = $fun;
   s/"$//;
   $value = $allfun{$fun} ;
   print "$fun [shape=rect,fontsize=6,label=$_\\n(".$value.")\"] \n" if 
$value > ($cutoff-1) ;
}

foreach $caller (keys %calltree){
   for $called ( keys %{ $calltree{$caller} } ) {
     $value = $calltree{$caller}{$called} ;
     $n1 = $allfun{$called} ;
     $n2 = $allfun{$caller} ;

     print " $caller -> $called [label=" . $value. ",fontsize=6]\n" if ( 
$value > $cutoff );
   }
}
print "}\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