Ok, this might be wierd or stupid.

I wrote a simple verbosity output handling sub that checks a global verbosity level and outputs to stdout. After profiling my script I realised that a huge amount of time was spent calling this function (I used it a lot) even when it wouldn't do anything because the global verbosity was so low.

So, I thought, OK I'll just use m4 and put a macro in my code so I can put syntax similer to how I called my sub into the perl code and run it through the m4 processor to get perl with the verbosity output logic written out every time. Now, there is no subroutine overhead...

However! I'm now writing in two languages, perl and m4. The way I have it set up, it looks deceptively like perl, but it isn't. The m4 interpreter does things I don't expect from time to time, so I'm looking for alternatives.

So! My questions for ye monks are:

Is there a way in perl to replace a call to a subroutine with inline code?

Is doing this actually improving my performance?

Does anyone have a better idea?

Details and examples below...

My script has the global $verbosity which is set by command line args.

Here's the subrutine version:

sub v_print { my $volume = shift; my $message = shift; if ( $volume >= $verbosity ) { print $message; } }

Called like this

v_print(1,"Holy Crap = $holy_crap\n");

Call that a zillion times in a performance sensitive script and it eats up a lot of resources.

So, I wrote this little bit of m4:

define(`v_print',`print $2 if $verbose >= $1')dnl

..and commented out the subrutine definition for v_print. Now, after running m4,

 v_print(1,"Holy Crap = $holy_crap\n");

...looks like...

print "Holy Crap = $holy_crap\n" if $verbose >= 1;

Only one of the many problems is that,

v_print(1,"Foo = $foo, Bar = $bar, Baz = $baz");

Turns into:

print "Foo = $foo if $verbose >= 1;

Thanks to how m4 handles quotes differently from perl...

Yes, that doesn't look like much of a savings in charcters typed, but this is a somewhat simplified example. The original also handled some logging and a few other features, so v_print(1,"Holy Crap = $holy_crap\n") is actually a lot shorter than the code produced by m4.

Update

It looks like Filter::cpp is the winner. The only way it could be better would be if the C preprocessor macros didn't use the same character as perl comments...


In reply to Macro in perl code? by pileofrogs

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.