Re: when to use variable vs. constant?



On Wed, Nov 12, 2008 at 4:10 PM, JC Janos <jcjanos245@xxxxxxxxx> wrote:
Chas,

On Wed, Nov 12, 2008 at 12:38 PM, Chas. Owens <chas.owens@xxxxxxxxx> wrote:
You may want to also look at Readonly*.

New one for me. Thanks.

$WHOIS1 can be modified (not good)

That's an easy point.

$WHOIS2 cannot be optimized away by the compiler (possible performance impact)

I have no idea what weight to give that optimization. Is it typically
large & very useful, or barely noticeable?


It's not rally as simple as it sounds: substitution is always being
performed--every occurrence of WHOIS or $WHOIS is being replaced by
your string--the only thing you are using the pragma to control is
whether the substitution happens at compile time or run time. In most
cases, it doesn't make a hill of beans. On my system, for instance,
Benchmark can't find any difference in a simple variable vs. constant
scenario under 1,000,000 iterations; I don't start to see the
improvement until I perform about 10,000,000 substitutions.

Where you really see a performance gain in real life is if you use a
constant value as a part of a condition that contains nothing but
other constant values. In that case the compiler will ignore any code
that cannot be evaluated. This is why you will often see 'use constant
DEBUG = 0' at the beginning of programs. That way debug code switched
as 'if (DEBUG) {...}' is never even compiled unless the program is in
debug mode. In a long, complex program, saving perhaps 1,000+ tests
can be a worthwhile optimization.

For most jobs, though, it makes more sense to think about
functionality, rather than an optimization that might or might not
gain you a fraction of a microsecond. Is there a chance that the
variable might be reset and cause unexpected results? Will
BIG_LETTERS_WITH_UNDERSCORES make your code more readable or easier to
maintain? If so, use constants.

In my experience, though, the convenience of interpolation wins out
99% of the time.

This is particularly true when automating system admin tasks. Once you
launch an external program, any optimizations to your Perl code will
be insignificant--statistically and otherwise--compared to the runtime
of the external system call.

HTH,

-- jay
--------------------------------------------------
This email and attachment(s): [ ] blogable; [ x ] ask first; [ ]
private and confidential

daggerquill [at] gmail [dot] com
http://www.tuaw.com http://www.downloadsquad.com http://www.engatiki.org

values of β will give rise to dom!


Relevant Pages

  • Re: copymemory basic question
    ... I have used that technique before; that is, put a MessageBox in a VC program ... workspace and then debug the program without using the MessageBox or MsgBox. ... > - Compile and save. ... not able to show anything due to the optimization. ...
    (microsoft.public.vb.winapi)
  • Re: debugging in release mode
    ... I have no doubt that it's a problem of mine and not of the compiler. ... I don't really need optimization ... for the sake of memory space or execution speed. ... shows the differences between the default debug mode and the default release ...
    (microsoft.public.vc.mfc)
  • RE: Step by Step skips large chunks of code
    ... project settings it is in debug mode and no optimization is set. ... We have several similar projects that debug fine, ... Microsoft Online Community Support ...
    (microsoft.public.vsnet.debugging)
  • Re: Downside of using Debug builds for production
    ... use of Microsoft's Exception Handling application block. ... the process if I desire to debug the application. ... For example, an "optimized" build could in some cases be larger than the "non-optimized" build, depending on what the optimization goals are. ... it seems to me that you are really only interested in one aspect of the "debug" build: the inclusion of symbols allowing for the stack trace to include line numbers in the exceptions. ...
    (microsoft.public.dotnet.framework)
  • Tricking pl1s production_table
    ... you're using debug and break statements to create a trace log. ... testprog: procedure options; ... I can't just go compile everything with -table ... which doesn't happen below optimization level 2. ...
    (comp.sys.stratus)