Which is better/faster?

Which is better/faster?

am 21.11.2005 20:17:28 von Ed Jay

I have a multi-module script that generates HTML pages using several text
strings common to each module and pages. There are six strings of about 50
characters each. I have two choices: I can write out the strings each time
they're used or I can define them and let Perl substitute them when
generating the pages. Which method is more efficient, i.e., faster?

--
Ed Jay (remove M to respond by email)

Re: Which is better/faster?

am 21.11.2005 21:33:34 von Paul Lalli

Ed Jay wrote:
> I have a multi-module script that generates HTML pages using several text
> strings common to each module and pages. There are six strings of about 50
> characters each. I have two choices: I can write out the strings each time
> they're used or I can define them and let Perl substitute them when
> generating the pages. Which method is more efficient, i.e., faster?

I think you have a very bizarre definition of "efficient". For me, it
would be far more efficient to do something once, rather than six
different times. Even if it is a microsecond faster[1] for Perl to use
a string literal rather than a string stored in a variable, who cares?
What happens when you suddenly (either through error, evolution, or
marketing) have to change one of those strings? If you hard code them
in, you have to find every place they're mentioned, and change each
instance.

Simply create a tiny module which defines your strings, and have any
chunk of code that needs them use that module and import those strings.

=============================
package MyStrings;
use strict;
use warnings;
use base 'Exporter';
our @EXPORT = qw/%config/;

%config = (
name=>'Paul D. Lalli',
address=>'123 Main St',
copyright=>'© 2005 LalliCo, Inc',
# etc ...
);

1;
==============================

==============================
#!/usr/bin/perl
use strict;
use warnings;
use MyStrings;
use CGI qw/:standard/;

print header;
print start_html("$config{name}'s homepage");
# etc
print div({id=>footer}, $config{copyright});
print end_html;
==============================

Paul Lalli

[1] I have no idea whether or not it would be a microsecond faster.
Use the Benchmark module to write your own tests to determine this
perldoc Benchmark;

Re: Which is better/faster?

am 21.11.2005 21:51:25 von Ed Jay

"Paul Lalli" wrote:

>Ed Jay wrote:
>> I have a multi-module script that generates HTML pages using several text
>> strings common to each module and pages. There are six strings of about 50
>> characters each. I have two choices: I can write out the strings each time
>> they're used or I can define them and let Perl substitute them when
>> generating the pages. Which method is more efficient, i.e., faster?
>
>I think you have a very bizarre definition of "efficient". For me, it
>would be far more efficient to do something once, rather than six
>different times. Even if it is a microsecond faster[1] for Perl to use
>a string literal rather than a string stored in a variable, who cares?
>What happens when you suddenly (either through error, evolution, or
>marketing) have to change one of those strings? If you hard code them
>in, you have to find every place they're mentioned, and change each
>instance.
>
>Simply create a tiny module which defines your strings, and have any
>chunk of code that needs them use that module and import those strings.
>
>=============================
>package MyStrings;
>use strict;
>use warnings;
>use base 'Exporter';
>our @EXPORT = qw/%config/;
>
>%config = (
> name=>'Paul D. Lalli',
> address=>'123 Main St',
> copyright=>'© 2005 LalliCo, Inc',
> # etc ...
>);
>
>1;
>==============================
>
>==============================
>#!/usr/bin/perl
>use strict;
>use warnings;
>use MyStrings;
>use CGI qw/:standard/;
>
>print header;
>print start_html("$config{name}'s homepage");
># etc
>print div({id=>footer}, $config{copyright});
>print end_html;
>==============================
>
>Paul Lalli
>
>[1] I have no idea whether or not it would be a microsecond faster.
>Use the Benchmark module to write your own tests to determine this
>perldoc Benchmark;

Thanks.

The issue isn't the extra msec processing time. The issue (to my novitiate
thinking) speaks to the amount of extra time to read in the script versus
the extra msecs of processing time. That said, I think your comment about
having to redo every instance of hard-coded literals says it all.

--
Ed Jay (remove M to respond by email)