For Programmers: Free Programming Magazines  


Home > Archive > AWK > January 2006 > awk arrays









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author awk arrays
Evie

2006-01-10, 3:58 am

Hello,

Is it more efficient (from a memory and cpu usage standpoint) to use
two 100,000 element arrays or one two-dimensional array (with each
dimension containing 100,000 elements)?

Evie

Janis Papanagnou

2006-01-10, 3:58 am

Evie wrote:
> Hello,
>
> Is it more efficient (from a memory and cpu usage standpoint) to use
> two 100,000 element arrays or one two-dimensional array (with each
> dimension containing 100,000 elements)?


The first case has 2*100,000 elements and the second one 100,000*100,000
elements; so that are completely different applications. If you fill all
data entries in both cases, the memory question is trivially answered.

But if you store the same amount of data in both structures then I would
expect the two-dimentional case to be slightly more time consuming since
your program will have to construct the index string for access from two
(or three) components, "ind-1" "SUBSEP" "ind-2". Otherwise it's the same
time efficiency, since awk arrays are associative and multi-dimensional
indices are mapped to a single index for access.

Janis
Loki Harfagr

2006-01-10, 3:58 am

Le Tue, 03 Jan 2006 00:06:25 +0100, Janis Papanagnou a écrit_:

> Evie wrote:
>
> The first case has 2*100,000 elements and the second one 100,000*100,000
> elements; so that are completely different applications.


I suppose the OP wanted to exprim:
"one two-dimensional array (with first dimension containing 100,000
elements and second dimension has two values" ?
Say, a vector(array) of doubletons, not a square.

Anyway, apart from the use of memory, your answer below:

> If you fill all
> data entries in both cases, the memory question is trivially answered.
>
> But if you store the same amount of data in both structures then I would
> expect the two-dimentional case to be slightly more time consuming since
> your program will have to construct the index string for access from two
> (or three) components, "ind-1" "SUBSEP" "ind-2". Otherwise it's the same
> time efficiency, since awk arrays are associative and multi-dimensional
> indices are mapped to a single index for access.
>
> Janis


is certainly correct from a general programming view, there
remains the possibility that some programs in awk are optimized for
these special, and reccurent, cases as it's frequent that in IT we deal
with bytes/doubles/and so on.
Though, I won't bet my nose on it :-) and I agree with you!

Evie

2006-01-10, 3:58 am

>> The first case has 2*100,000 elements and the second one 100,000*100,000
[color=darkred]
> I suppose the OP wanted to exprim:
> "one two-dimensional array (with first dimension containing 100,000
> elements and second dimension has two values" ?
> Say, a vector(array) of doubletons, not a square.


As you both figured out, I did mean to ask about two arrays with the
same number of elements. Since the difference is not that great, I
think I'll use the two-dimensional array. In this case, it makes the
code easier to read.

Thanks,
Evie

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com