Re: String or Numeric Variable?



On Fri, 28 Oct 2005 14:59:11 -0500, "ProfitMaxTrading.com"
<nospam@xxxxxxxxxx> wrote:

>Hi Mike.
>
>Thanks for your comments.
>
>Okay, it appears that using numeric constants will solve the 'readability'
>issue and Enum (Fernando's comment) perhaps more efficient? So I'll go with
>the numeric route rather than string because I have a ton of these
>IF..THENS that work off these all through the program.

Considering your usage CONSTs or ENUMs are definitely a good choice for
readability, efficiency, etc. I believe CONSTs, and therefore checks
against CONSTs, are optimized in the compiled EXE, whereas variables are
not (and the ENUM would rely on a variable).

I would think that in your proposed usage it would not make much
difference if you used;

Const Morning As Long = 1 ' or BYTE
Const Afternoon As Long = 2
Const Evening As Long = 3

If TimeOfDay = Morning Then ...

....or...

Enum TOD
Morning = 1
Afternoon = 2
Evening = 3
End Enum
Dim TimeOfDay as TOD

If TimeOfDay = Morning Then ...
>
>But you mentioned LONG as opposed to BYTE or INTEGER. Are you saying that a
>LONG type is better for even single-digit numbers used for nothing more than
>comparisons (as opposed to having math performed on them) than a BYTE or
>INTEGER type?
>
>Obviously the BYTE or INTEGER type would use less memory. But I suppose not
>a big deal if you don't over do it. But just why is LONG better?
>
LONGs are generally more efficient (in 32-bit environments) when used in
calculations, so if for instance you were ANDing or ORing your values
for some reason to get combinations of TimeOfDay you might see some
difference (definitely if performing math functions on them).

If memory is likely to be an issue then use the lowest datatype you need
to support your min/max possible values, but if not it never hurts to
use a LONG (I've not tried any profiling but I wonder if a LONG CONST
produces any lesser/greater optimisation than a BYTE CONST ?).

ENUM values are always declared as LONGs by the way (hence no datatype
specifier).
--
Alfie
<http://www.delphia.co.uk/>
Steinbach's Guideline for Systems Programming: Never test for an error condition you don't know how to handle.

.



Relevant Pages

  • [PATCH 8/10] Re: [2.6-BK-URL] NTFS: 2.1.19 sparse annotation, cleanups and a bugfix
    ... -static int ntfs_attr_find(const ATTR_TYPES type, const ntfschar *name, ... -typedef enum { ... +typedef le32 NTFS_RECORD_TYPE; ... These are the so far known MFT_RECORD_* flags which contain ...
    (Linux-Kernel)
  • Re: wanna be expert
    ... ...where SOME_OPTIONS is a const or enum, depending on type requirements. ... In reality a lot of programmers use literals instead of named constants ...
    (comp.programming)
  • Re: String or Numeric Variable?
    ... it appears that using numeric constants will solve the 'readability' ... >>issue and Enum perhaps more efficient? ... > Const Afternoon As Long = 2 ... > Dim TimeOfDay as TOD ...
    (comp.lang.basic.visual.misc)
  • Re: Advantage of const against static readonly?
    ... >>You aren't worried about the memory consumption of a static readonly value ... > built on readonly static fields instead of consts you could probably ... remove the definition of the enum from the library it is using. ... But I have indeed found one reason why const can be better than static ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Init structure of constants
    ... >>> How should I define a structure of const to switch on. ... you need compile time constants. ... >> enum myConstantsWithPurpose { ...
    (comp.lang.c)