For Programmers: Free Programming Magazines  


Home > Archive > PERL Programming > November 2005 > Which is better/faster?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Which is better/faster?
Ed Jay

2005-11-22, 3:56 am

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)
Paul Lalli

2005-11-22, 3:56 am

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;

Ed Jay

2005-11-22, 3:56 am

"Paul Lalli" <mritty@gmail.com> wrote:

>Ed Jay wrote:
>
>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)
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com