From: Bart Schaefer <schaefer@brasslantern.com>
To: zsh-users@zsh.org
Subject: Re: Why large arrays are extremely slow to handle?
Date: Thu, 24 Mar 2011 19:29:14 -0700 [thread overview]
Message-ID: <110324192914.ZM29861@torch.brasslantern.com> (raw)
In-Reply-To: <5c830d92034e61a01c96e6aa4d10798c.squirrel@gameframe.net>
On Mar 25, 2:37am, nix@myproxylists.com wrote:
}
} I think there's is a big flaw somewhere that causes the following:
}
} #!/bin/zsh
} emulate zsh
} TEST=()
} for i in {1..10000} ; do
} TEST+="$i" # append (push) to an array
} done
}
} --- 10K
} time ./bench
} real 0m3.944s
}
} --- 50K BOOOM! WTF?
}
} time ./bench
} real 1m53.321s
}
} Any ideas why it's extremely slow?
It's not the array, it's the loop interpretation thereof.
TEST=({1..50000})
will populate a 50k-element array almost instantly. Here's a 500,000
element array on my home desktop:
torch% typeset -F SECONDS
torch% print $SECONDS; TEST=({1..500000}); print $SECONDS
24.9600260000
25.4452710000
torch%
Put that in a loop instead, and you're interpreting a fetch/replace of the
whole array on every cycle. This is in part because array assignment is
generalized for replacing arbitrary slices of the array; append is not
treated specially. [If someone wants to try to optimize this, start at
the final "else" block in Src/params.c : setarrvalue() -- but beware of
what happens in freearray().]
As it happens, you can get much better update performance at the cost of
some memory performance by using an associative array instead. Try:
typeset -A TEST
for i in {1..50000} ; do
TEST[$i]=$i
done
Individual elements of hashes *are* fetched by reference without the
whole hash coming along, and are updated in place rather than treated
as slices, so this is your fastest option without a C-code change.
You can also build up the "array" as a simple text block with delimiters,
then split it to an actual array very quickly. Append to a scalar isn't
really any better algorithmically than an array, but it does fewer memory
operations.
torch% for i in {1..50000}; do TEST+="$i"$'\n' ; done
torch% TEST=(${(f)TEST})
torch% print $#TEST
50000
next prev parent reply other threads:[~2011-03-25 2:29 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-03-25 0:37 nix
2011-03-25 0:46 ` Mikael Magnusson
2011-03-25 1:12 ` nix
2011-03-25 2:29 ` Bart Schaefer [this message]
2011-03-25 2:50 ` nix
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=110324192914.ZM29861@torch.brasslantern.com \
--to=schaefer@brasslantern.com \
--cc=zsh-users@zsh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).