Optimization of the code - suggestions? 
Author Message
 Optimization of the code - suggestions?

Hi,

I'm here to ask for some optimization on the code which i have written
in Perl. My design flow is this way. I have a main library file, which
has all the COMMON subroutines, called from more than 10 different
files. I have designed the library file in such a way that even a
single "if condition" will be checked in the library file only, based
on the parameters passed. So instead of writing the same if condition
in 10 different files, i have it in this library file and all the 10
files will call this if condition based on the parameters. But there
is an additional cost involved for this reusability of code. Since i
do some hash manupulations and flag setting in the "if condition"
everytime i call the subroutine i need to pass the hashreference, flag
etc., and return the hashreference and other flags.

My questions are,
1. Is it advisable to call library file often from 10 different files?
If i count, around 10 calls to the library is happening from each
file.
2. Or I shouldn't bother about redundancy of code and should keep the
small check like if , foreach in the respective files?? Should i go
for re-usability or keeping the manupulations involved in mind, i
should NOT consider re-usability?? If i don't use re-usability,
maintenence will be a pain. For every change in business logic we need
to rewrite the loops, conditions. And there are plenty of chances that
business logic might chance because the client is still not clear
about his business logic.

NOTE: The library file is a Perl MODULE (.pm) and defined in XYZ
namespace. The other 10 files are ordinary .cgi or .pl files which
uses the MODULE.

Thanks in advance,
- Venu



Mon, 18 Oct 2004 06:26:17 GMT  
 Optimization of the code - suggestions?

Quote:
> Hi,

> I'm here to ask for some optimization on the code which i have written
> in Perl. My design flow is this way. I have a main library file, which
> has all the COMMON subroutines, called from more than 10 different
> files. I have designed the library file in such a way that even a
> single "if condition" will be checked in the library file only, based
> on the parameters passed. So instead of writing the same if condition
> in 10 different files, i have it in this library file and all the 10
> files will call this if condition based on the parameters. But there

What parameters, and how are they passed to the library file?

Quote:
> is an additional cost involved for this reusability of code. Since i
> do some hash manupulations and flag setting in the "if condition"
> everytime i call the subroutine i need to pass the hashreference, flag
> etc., and return the hashreference and other flags.

Efficiency aside, does it even work as intended?  When you "require"
or "use" a Perl module multiple times in a single Perl run, the
module code is only read and executed the first time, so multiple
calls are rather useless.  Only an import() routine will be called
each time if you have provided one.  From your description it doesn't
seem like you did.

In fact, from your description it isn't entirely clear what you are
really doing.  Please show us some code.

Anno



Mon, 18 Oct 2004 11:22:41 GMT  
 Optimization of the code - suggestions?

Quote:

>So instead of writing the same if condition
>in 10 different files, i have it in this library file and all the 10
>files will call this if condition based on the parameters.

That's how it should be done.

Quote:
>But there
>is an additional cost involved for this reusability of code. Since i
>do some hash manupulations and flag setting in the "if condition"
>everytime i call the subroutine i need to pass the hashreference, flag
>etc., and return the hashreference and other flags.

If you don't change the hash reference, thus you don't make a copy of
the hash, but instead you modify the original hash, then you needn't
even return the hash reference. Just the flags, ma'm.

Quote:
>My questions are,
>1. Is it advisable to call library file often from 10 different files?
>If i count, around 10 calls to the library is happening from each
>file.

Yes. The module will be loaded only once. Once it's loaded (thus,
compiled) it makes no difference if the code resides in a separate
module, or is included in your script. Not one shred of difference.

Quote:
>2. Or I shouldn't bother about redundancy of code and should keep the
>small check like if , foreach in the respective files?? Should i go
>for re-usability or keeping the manupulations involved in mind, i
>should NOT consider re-usability?? If i don't use re-usability,
>maintenence will be a pain. For every change in business logic we need
>to rewrite the loops, conditions. And there are plenty of chances that
>business logic might chance because the client is still not clear
>about his business logic.

That's why you should go for reusability. Don't worry about
"efficiency", I think you're actually talking about a difference in
speed in the range of microseconds only.

Quote:
>NOTE: The library file is a Perl MODULE (.pm) and defined in XYZ
>namespace. The other 10 files are ordinary .cgi or .pl files which
>uses the MODULE.

That's what I gathered.

--
        Bart.



Mon, 18 Oct 2004 13:21:45 GMT  
 Optimization of the code - suggestions?

Hi Anno,

Thanks for the reply.

Quote:
> What parameters, and how are they passed to the library file?

I pass two scalar flag variables, one hash ref and return the scalar
flags modified depending on the condition? Or is it efficient to pass
scalar flags as reference too, to avoid the formal, actual parameters
processing?? Suggest..

Quote:
> Efficiency aside, does it even work as intended?  When you "require"
> or "use" a Perl module multiple times in a single Perl run, the
> module code is only read and executed the first time, so multiple
> calls are rather useless.  Only an import() routine will be called
> each time if you have provided one.  From your description it doesn't
> seem like you did.

I doesn't mean to "use" or "require" multiple times. I just did "use
<library>". But i call the subroutines in <library> around 10 times.
My actual question is..Consider the following check for example the
piece of code

my $a = shift;
my $flag = 0;
$flag = 1 if ($a < 10);

has to be checked for all the 10 files. So i have decided to place it
in <library> file.

But is it efficient, placing this code inline in respective files or
calling a <library> file subroutine?? In brief, will the cost of
calling a subroutine in a file places somewhere in the path is
efficient than writing the same code inline??

I have mentioned a simple if condition check, but the check has will
be doing more things.

Quote:

> In fact, from your description it isn't entirely clear what you are
> really doing.  Please show us some code.

I hope the above example makes you clear?

- Venu



Tue, 19 Oct 2004 07:59:04 GMT  
 Optimization of the code - suggestions?

Quote:

> If you don't change the hash reference, thus you don't make a copy of
> the hash, but instead you modify the original hash, then you needn't
> even return the hash reference. Just the flags, ma'm.

Please tell me what is wrong in the following program :(

     1  #!/bin/perl -w
     2
     3  use strict;
     4
     5  my %h = ();
     6  my ($k, $v);
     7
     8  %h = %{&change(\%h)};
     9
    10  while (($k,$v) = each(%h))
    11  {
    12          print "$k : $v\n";
    13  }
    14
    15  print "Second call\n";
    16
    17  %h = %{&change1(\%h)};
    18
    19  while (($k,$v) = each(%h))
    20  {
    21          print "$k : $v\n";
    22  }
    23
    24  sub change
    25  {
    26          my ($href) = shift;
    27          my %hderef = %$href;
    28          $hderef{$_} = $_++ for(1..6);
    29          return \%hderef;
    30  }
    31
    32  sub change1
    33  {
    34          my ($href) = shift;
    35          my %hderef = %$href;
    36          $hderef{$_} = $_*2 for(1..6);
    37          return \%hderef;
    38  }

As per your above argument..If i remove the %h on LHS on line 17 also
it should print the changed value (since i called change1() and %h is
changed in change1()). Instead it is printing the old %h only.

sub change - will actually increment the values and keep.
sub change1 - will double and store the values

Can you tell me why that program behaves like that?

I can see one reason..
Is it that i'm taking the parameter in the local (my) variable and
returning the reference to that hash to the original calling code?
There are plenty of chances for this.

I tried to troubleshoot the above problem. I made following changes
i) commented 26 & 34 lines
ii) replaced 27 & 35 lines with "my %hderef = %{shift}" .

But it still throws the following error:

Ambiguous use of %{shift} resolved to %shift at change_hash.cgi line
....
Global symbol "%shift" requires explicit package name at
change_hash.cgi line .....
Execution of change_hash.cgi aborted due to compilation errors.

What should i do to solve this?

Thanks in advance,
- Venu



Tue, 19 Oct 2004 08:14:58 GMT  
 Optimization of the code - suggestions?

Quote:

> Hi Anno,

> Thanks for the reply.

> > What parameters, and how are they passed to the library file?

> I pass two scalar flag variables, one hash ref and return the scalar
> flags modified depending on the condition? Or is it efficient to pass
> scalar flags as reference too, to avoid the formal, actual parameters
> processing?? Suggest..

I suggest you don't worry about it.  The difference between accessing
a scalar directly and through a reference is tiny.  If anything, the
reference is going to be slower, but it will be hardly measurable.

BTW, you are not passing the parameters to the library file, you are
passing them to a routine (which happens to be defined in the library).

Quote:
> > Efficiency aside, does it even work as intended?  When you "require"
> > or "use" a Perl module multiple times in a single Perl run, the
> > module code is only read and executed the first time, so multiple
> > calls are rather useless.  Only an import() routine will be called
> > each time if you have provided one.  From your description it doesn't
> > seem like you did.

> I doesn't mean to "use" or "require" multiple times. I just did "use

Yes.  I misread your initial posting.

Quote:
> <library>". But i call the subroutines in <library> around 10 times.
> My actual question is..Consider the following check for example the
> piece of code

> my $a = shift;
> my $flag = 0;
> $flag = 1 if ($a < 10);

> has to be checked for all the 10 files. So i have decided to place it
> in <library> file.

> But is it efficient, placing this code inline in respective files or
> calling a <library> file subroutine?? In brief, will the cost of
> calling a subroutine in a file places somewhere in the path is
> efficient than writing the same code inline??

Don't worry about runtime efficiency so much.  In fact, at this
early stage, don't worry about it at all.  Get the program right.
Then, if it's slow, make it faster.

Nevertheless, if the same code appears multiple times in a number
of scripts, that is a very good reason to put it in a library.
The reason is not runtime efficiency but programmer efficiency:
you will only have one place where to check and possibly modify
the common code.

Quote:
> I have mentioned a simple if condition check, but the check has will
> be doing more things.

More reason to put it in a library.  The more code you have, the more
likely it is that you will have to change it at some point.  You
don't want to walk through then files and make the same change in
each one.  A library avoids this.

Anno



Tue, 19 Oct 2004 09:20:28 GMT  
 Optimization of the code - suggestions?

Quote:

> Hi Anno,

> Thanks for the reply.

> > What parameters, and how are they passed to the library file?

> I pass two scalar flag variables, one hash ref and return the scalar
> flags modified depending on the condition? Or is it efficient to pass
> scalar flags as reference too, to avoid the formal, actual parameters
> processing?? Suggest..

I suggest you don't worry about it.  The difference between accessing
a scalar directly and through a reference is tiny.  If anything, the
reference is going to be slower, but it will be hardly measurable.

BTW, you are not passing the parameters to the library file, you are
passing them to a routine (which happens to be defined in the library).

Quote:
> > Efficiency aside, does it even work as intended?  When you "require"
> > or "use" a Perl module multiple times in a single Perl run, the
> > module code is only read and executed the first time, so multiple
> > calls are rather useless.  Only an import() routine will be called
> > each time if you have provided one.  From your description it doesn't
> > seem like you did.

> I doesn't mean to "use" or "require" multiple times. I just did "use

Yes.  I misread your initial posting.

Quote:
> <library>". But i call the subroutines in <library> around 10 times.
> My actual question is..Consider the following check for example the
> piece of code

> my $a = shift;
> my $flag = 0;
> $flag = 1 if ($a < 10);

> has to be checked for all the 10 files. So i have decided to place it
> in <library> file.

> But is it efficient, placing this code inline in respective files or
> calling a <library> file subroutine?? In brief, will the cost of
> calling a subroutine in a file places somewhere in the path is
> efficient than writing the same code inline??

Don't worry about runtime efficiency so much.  In fact, at this
early stage, don't worry about it at all.  Get the program right.
Then, if it's slow, make it faster.

Nevertheless, if the same code appears multiple times in a number
of scripts, that is a very good reason to put it in a library.
The reason is not runtime efficiency but programmer efficiency:
you will only have one place where to check and possibly modify
the common code.

Quote:
> I have mentioned a simple if condition check, but the check has will
> be doing more things.

More reason to put it in a library.  The more code you have, the more
likely it is that you will have to change it at some point.  You
don't want to walk through ten files and make the same change in
each one.  A library avoids this.

Anno



Tue, 19 Oct 2004 09:44:57 GMT  
 Optimization of the code - suggestions?

Quote:


>> If you don't change the hash reference, thus you don't make a copy of
>> the hash, but instead you modify the original hash, then you needn't
>> even return the hash reference. Just the flags, ma'm.

>Please tell me what is wrong in the following program :(

The following program does not do what Bart suggests you do...

Quote:
>    24  sub change
>    25  {
>    26          my ($href) = shift;
>    27          my %hderef = %$href;
>    28          $hderef{$_} = $_++ for(1..6);

                               ^^^^

Should be "$_ + 1" instead.

(I think that is the answer to your actual question...)

Quote:
>    29          return \%hderef;
>    30  }

You are still operating on a copy and returning the copy.

Bart suggests you use the hash ref directly to modify the hash,
then there is no need to return anything. Something like:

  change(\%h);
  ...
  sub change
  {
          my ($href) = shift;
          $href->{$_} = $_+1 for(1..6);
  }

Quote:
>As per your above argument..

 ^^^^^^      ^^^^^

Your code does not implement what Bart argued above!

Quote:
>Is it that i'm taking the parameter in the local (my) variable and

                                            ^^^^^

"local" means something in Perl. It means dynamic variable.

my() variables are lexical variables.

You should not use "local" when referring to my() variables,
despite the fact that Perl's use of the word "local" is loco.  :-)

Quote:
>returning the reference to that hash to the original calling code?

Yes. That is what Bart suggest you can avoid, but your code
does not yet avoid it.

--
    Tad McClellan                          SGML consulting

    Fort Worth, Texas



Tue, 19 Oct 2004 16:25:24 GMT  
 Optimization of the code - suggestions?

Quote:

> >    24  sub change
> >    25  {
> >    26          my ($href) = shift;
> >    27          my %hderef = %$href;
> >    28          $hderef{$_} = $_++ for(1..6);
>                                ^^^^

> Should be "$_ + 1" instead.

> (I think that is the answer to your actual question...)

Hi Tad,

Thanks for your patient explanation. But what is the difference
between $_++ and $_+ 1 ? The output is different when i use each of
the above form. Why is that? $_++ is intenally $_+1 right? Then what
is the difference? Pls clarify.

Thanks in advance,
- Venu



Fri, 22 Oct 2004 04:56:51 GMT  
 Optimization of the code - suggestions?
[snip]

Quote:
> $_++ is intenally $_+1 right?

No.  Read perldoc perlop

--
print reverse( ",rekcah", " lreP", " rehtona", " tsuJ" )."\n";



Fri, 22 Oct 2004 05:13:13 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. Need suggestions on optimization

2. Optimization of Perl code

3. code optimization and readability

4. Code optimization request and i18n issues

5. Code Optimization-- Can I do this better?

6. Code Optimization-- Can I do this better?

7. Code optimization: Anyone got a few spare minutes?

8. Optimization of date calculation code

9. Suggestion wanted on improving code.

10. possible perl optimizations

11. optimization help

12. Optimization help...

 

 
Powered by phpBB® Forum Software