sifukurt has asked for the wisdom of the Perl Monks concerning the following question:
I've got a module that I'm working on where I am pre-caching a number of very large constants, created with Math::BigFloat. My problem is this: I've got 10 or 12 of these very large constants, but for the most part, only one or two of them are going to used at any given time. That being the case, I don't want to automatically dump all of them into memory, only the ones that a user asks for. For example, I've got the square root of 2 and the square root of 5 ($_sqrt2_ and $_sqrt5_, respectively) as two of these constants. Let's say I only need $_sqrt2_. What would be the best method for creating these via Math::BigFloat "on demand?" My first thought was to make a CONSTANT() routine where the user would pass the name of the constant they want and have it created at that time. It seems to me, though, that there should be a better way. I did some digging through the search here, but didn't find anything (perhaps I was searching for the wrong thing?). Any suggestions?
Many thanks.
___________________
Kurt
UPDATE (11-Feb-2002 10:21:38 AM): By way of further clarification, the problem isn't that I'm concerned about the calculation time of these constants. I'm pre-calculating them and I'm subsequently hard-coding them as Math::BigFloat objects. My concern is a memory issue since some of these constants are very, very large. I suppose a few extra kb of memory probably isn't going to make much of a difference, but it is The Principle of The Thing. My original plan was to use an accessor method similar to that described by hossman to avoid dumping all of these constants into memory if only one or two (or possibly even none) are going to be used in a given script. Forgive me for showing my ignorance here, but does the Tie::Scalar method that japhy used accomplish this? I've never used Tie::Scalar before, so I'm a little in the dark, even after reading the docs.
Many thanks.
___________________
Kurt
UPDATE (11-Feb-2002 10:21:38 AM): By way of further clarification, the problem isn't that I'm concerned about the calculation time of these constants. I'm pre-calculating them and I'm subsequently hard-coding them as Math::BigFloat objects. My concern is a memory issue since some of these constants are very, very large. I suppose a few extra kb of memory probably isn't going to make much of a difference, but it is The Principle of The Thing. My original plan was to use an accessor method similar to that described by hossman to avoid dumping all of these constants into memory if only one or two (or possibly even none) are going to be used in a given script. Forgive me for showing my ignorance here, but does the Tie::Scalar method that japhy used accomplish this? I've never used Tie::Scalar before, so I'm a little in the dark, even after reading the docs.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Pre-caching large constants
by japhy (Canon) on Feb 08, 2002 at 22:12 UTC | |
by hossman (Prior) on Feb 09, 2002 at 22:34 UTC | |
Re: Pre-caching large constants
by chromatic (Archbishop) on Feb 08, 2002 at 21:26 UTC | |
(bbfu) (Memoize) Re: Pre-caching large constants
by bbfu (Curate) on Feb 09, 2002 at 02:02 UTC | |
Re: Pre-caching large constants
by runrig (Abbot) on Feb 08, 2002 at 22:53 UTC | |
Re: Pre-caching large constants
by lestrrat (Deacon) on Feb 08, 2002 at 21:22 UTC | |
by hossman (Prior) on Feb 08, 2002 at 22:21 UTC | |
by lestrrat (Deacon) on Feb 08, 2002 at 22:34 UTC |
Back to
Seekers of Perl Wisdom