Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Thursday November 17, 2005
02:45 PM

Counting Op Codes

[ #27616 ]

I've wanted to play with B::Utils for a while, so I wrote a little op code counter. I'm not drawing any conclusions from their counts, though:

#!/usr/bin/perl -w
use strict;
 
require B::Utils;
 
die "Specify some modules on the command line\n" unless @ARGV;
 
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Load the modules
my %Modules = ();
 
foreach my $module ( @ARGV )
    {
    $Modules{ $module }++;
    eval "require $module";
    }
 
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Get all the roots, but delete the ones not under our namespaces
my %op_roots = B::Utils::all_roots();
 
# there may be sub-modules, so match on their root name
my $regex =
    '^(' .
    join( "|", map quotemeta($_), keys %Modules ) .
    ')';
 
foreach my $key ( keys %op_roots )
    {
    next if $key =~ /$regex/;
    delete $op_roots{ $key };
    }
 
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Count the opcodes in the remaining roots
my %op_codes = ();
 
foreach my $key ( keys %op_roots )
    {
    B::Utils::walkoptree_simple(
        $op_roots{ $key },
        sub { $op_codes{ $key }++ }
        );
    }
 
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Display the reports
print "Reporting for @ARGV\n\n";
print " OpCodes | Function\n";
print "-" x 9, "|", "-" x 63, "\n";
 
foreach( sort { $op_codes{$b} <=> $op_codes{$a} } keys %op_roots )
    {
    printf " %7d | %s\n", $op_codes{$_}, $_;
    }
 
print "-" x 73,  "\n";

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.