Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

arrays instead of lists

25 views
Skip to first unread message

Sean McIlroy

unread,
Feb 20, 2012, 3:49:29 AM2/20/12
to
hello

how much more efficient is it to use (one-dimensional) arrays instead
of (long) lists? could it conceiveably make the difference between a
script that overflows the stack and one that doesn't? thanks if you
can help

peace
stm

Paul Rubin

unread,
Feb 20, 2012, 4:17:07 AM2/20/12
to
Stack overflow is usually caused by space leaks due to lazy evaluation
letting unforced thunks pile up. It takes a while to develop a sense of
where this is happening and put in annotations to prevent it. One easy
measure is to compile with ghc -O2 and see if that helps (it does more
automatic analysis of this issue).

Once you have the space leaks out of the way, you shouldn't get stack
overflow unless the program uses an awful lot of memory. Arrays can be
faster than lists or intmaps, but it's best to think of them as a
low-level optimization to pursue after you've gotten algorithmic issues
out of the way.

Dirk Thierbach

unread,
Feb 20, 2012, 5:27:41 AM2/20/12
to
Sean McIlroy <namenob...@gmail.com> wrote:
> how much more efficient is it to use (one-dimensional) arrays instead
> of (long) lists?

That depends very much on how you use the list. If it's a lazy list
that represents e.g. an ongoing computation, an array is less efficient.
If it's a strict list that can be, for example, processed in chunks,
it's more efficient (less memory, better cache usage).

Data.ByteString is an example for the latter case.

> could it conceiveably make the difference between a script that
> overflows the stack and one that doesn't?

Unlikely. If that is your actual question, why don't you post some
(condensed) code that exhibits that problem?

- Dirk
0 new messages