1

I am using mod-perl. I am under such a impression that our variable will not be created for every execution. i.e. If I create an hash variable as our then that varible will be created once and will remain in memory cache of apache for subsequent run.

So my question is will there be any difference in execution speed for below two in mod-perl?

Module1
....
....
our %myhash = qw ( list of key value );
...
....
sub fun() {
  if(exists $myhash{'key'}) {
  ...................
   return ;
}

and

Module2
.....
.....
sub fun() {
  my %myhash = qw ( list of key value );
  if(exists $myhash{'key'}) {
  ...................
   return ;
}

Which one is better in term of speed of execution on mod-perl if I am invoking this function once for each run ?

Gaurav Pant
  • 4,029
  • 6
  • 31
  • 54

2 Answers2

1

All static variables, including global and package scoped, will get initialised just once. So if you have large data structures to set up which will be used in many requests then it is much more efficient to initialise them statically.

You do not need to declare them using 'our'. If they are only referenced in the one package then you can use 'my' (outside all subs of course) to minimise the risk of a name collision.

harmic
  • 28,606
  • 5
  • 67
  • 91
0

I will go with Module1 as it is creating a global variable which is created once and can be accessed by subroutine(s) rather than creating every time a subroutine is called.

HackerKarma
  • 620
  • 7
  • 18
  • What if i am using this function once for single run. not two times? – Gaurav Pant Feb 19 '14 at 05:58
  • In that case, it doesn't make that difference. It depends on the use case. Often, some variables are maintained globally which offer a big benefit to a program's maintainability. But, I generally try to keep global vars to the minimal. – HackerKarma Feb 19 '14 at 06:03