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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |