Solve a Debate

W

Wolfgang Draxinger

days_in_month 12:
31
30
28
31
...
30
31
assign $days days_in_month[$month]

This is missing
days_in_month 12:
31
break
30
break

What shall there be missing? breaks? You noticed, that I defined
some artificial architecture on purpose. "days_in_month 12:"
tells it, that the next 12 blurps are tabular data, that can be
indexed. If the "interpreter" hits the line "days_in_month 12:"
it will unconditionally jump 12 instructions forward, where it
hits the assign instruction, which assignes a value from a table
by an index given into a variable. We're not talking about
Python here.

I introduced this "architecture" just to showcase that even in
the most "optimal" situation, where special RISC operations are
avaliable a LUT is still better suited.

Wolfgang Draxinger
 
C

castironpi

What shall there be missing? breaks? You noticed, that I defined
some artificial architecture on purpose. "days_in_month 12:"
tells it, that the next 12 blurps are tabular data, that can be
indexed. If the "interpreter" hits the line "days_in_month 12:"
it will unconditionally jump 12 instructions forward, where it
hits the assign instruction, which assignes a value from a table
by an index given into a variable. We're not talking about
Python here.

I introduced this "architecture" just to showcase that even in
the most "optimal" situation, where special RISC operations are
avaliable a LUT is still better suited.

Month lengths are tabular, as opposed to formulaic.
 
G

greg

Wolfgang said:
Somehow you seem to think, that a lookup table will require more
resources (memory I guess you thought) than a sequence of
comparisons. However you didn't take into account, that the
program code itself requires memory, too (for the operation
codes).

In Python, there's the additional twist that for
lookup tables based on a mutable type, such as a
dictionary, you need to execute code to build the
dictionary. So you end up using very roughly twice
the memory -- for the code to build the dictionary,
and the dictionary itself.

If the dictionary isn't too huge, and it's looked
up many times during each run of the program, the
extra speed of the dict lookup is probably worth
the overhead of creating it.

If the dict is very large, and is consulted
relatively few times in a given run, then it might
well be faster and not use too much more memory to
use a tree (NOT a linear sequence!) of if-else
statements in the code.

You could also consider loading the dict from some
other form, such as a pickle, instead of creating
it using code. This would use less memory, although
probably would take roughly the same time to set
up the table.

For extremely large infrequently-used tables, it's
probably better to look at not loading the whole
table into memory at all, and keeping it in some
kind of external indexed structure such as a b-tree,
relational database, etc.

In other languages, the tradeoffs will be completely
different. E.g. in C, if you can describe the table
entirely with static data, it'll be very fast to
load and incur no overhead for code to create it at
all. Also, with demand-paging out of the executable
file, and infrequent lookups, only parts of the
table will actually get loaded anyway.
 
C

castironpi

In Python, there's the additional twist that for
lookup tables based on a mutable type, such as a
dictionary, you need to execute code to build the
dictionary. So you end up using very roughly twice
the memory -- for the code to build the dictionary,
and the dictionary itself.

If the dictionary isn't too huge, and it's looked
up many times during each run of the program, the
extra speed of the dict lookup is probably worth
the overhead of creating it.

If the dict is very large, and is consulted
relatively few times in a given run, then it might
well be faster and not use too much more memory to
use a tree (NOT a linear sequence!) of if-else
statements in the code.

You could also consider loading the dict from some
other form, such as a pickle, instead of creating
it using code. This would use less memory, although
probably would take roughly the same time to set
up the table.

For extremely large infrequently-used tables, it's
probably better to look at not loading the whole
table into memory at all, and keeping it in some
kind of external indexed structure such as a b-tree,
relational database, etc.

In other languages, the tradeoffs will be completely
different. E.g. in C, if you can describe the table
entirely with static data, it'll be very fast to
load and incur no overhead for code to create it at
all. Also, with demand-paging out of the executable
file, and infrequent lookups, only parts of the
table will actually get loaded anyway.

Past a "many-small" certain point on numbers of hash-tables, if that's
the right word, in a program, and intepreter process on a machine, is
it be more time-efficient to allocate a 2**32-byte table? Are
'modulo' and 'doublesize' the only steps of the lookup process that it
would eliminate, and are they expensive ones? If so, what point? If
not, what's a tighter bottleneck?
 
C

castironpi

Past a "many-small" certain point on numbers of hash-tables, if that's
the right word, in a program, and intepreter process on a machine, is
it be more time-efficient to allocate a 2**32-byte table?  Are
'modulo' and 'doublesize' the only steps of the lookup process that it
would eliminate, and are they expensive ones?  If so, what point?  If
not, what's a tighter bottleneck?- Hide quoted text -

- Show quoted text -

There's something to it. As stated, your idea cannot be implemented
with current assumptions that Python makes, specifically that integers
are their own hash values, possible others. Relax this, and you may
be on to something, have a path, such as keying the object id into the
hash value.
 
C

castironpi

days_in_month 12:
31
30
28
31
...
30
31
assign $days days_in_month[$month]

This program consists of 2 operations (table jump and assignment)
and 12 values. This makes a memory consumption of 12+2 = 14

Along the same lines, you could populate the table somewhat sparsely,
and goto a modulo key.

You could even put functions in it:

setreturn $endoftable
hash month
goto
-0
-1
-2
jan.-> 3: setvjmp janfun
-4
-5
apr.-> 6: setvjmp aprfun
$endoftable

Sweet! Are you stack-ops?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,150
Latest member
MakersCBDReviews
Top