in reply to How do I measure my bottle ?
Almost all of those extra 5 seconds is spent allocating and reallocating memory as the hash is extended.
In theory, you could pre-extend your hash by using keys as an lvalue: keys %hash = 1234567;.
In reality, there are two problems with this.
If you run the following 1-liner:
perl -e"keys %hash = 2_000_000; <>; $hash{ $_ }++ for 1 .. 2_000_000; +<>;"
When activity stops at the first input, the space allocated to the process will be around 18 MB.
If you then hit enter, the hash is populated and the space requirement grows to around 192 MB.
Most of the time is spent allocating the space for the individual keys and values, not the buckets (it seems?).
In the past, I've tried various schemes to try and grab the required space from the OS in a single chunk rather than in zillions of iddy-biddy bits, but I haven't found a way of doing this using the memory allocator used by the AS built perl's.
You can very quickly grab a large chunk of memory from the OS using 'X' x ( 200*1024**2); for example.
D:\>perl -we"'X'x(200*1024**2); <>; $h{ $_ }++ for 1 .. 2_000_000;<>" Useless use of repeat (x) in void context at -e line 1. Name "main::h" used only once: possible typo at -e line 1.
Despite the "Useless use of repeat (x) in void context" message, you'll see that at the first prompt, the requried 200 MB of space has been allocated to the process (almost instantly), but when you hit enter, the process then goes on to grab 200MB more space.
Despite that the initial 200 MB was never assigned to anything--that space is never reused. I've tried in vain to find a way to pursuade perl to reuse it.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How do I measure my bottle ?
by cbrain (Novice) on Mar 25, 2005 at 16:32 UTC | |
by BrowserUk (Patriarch) on Mar 25, 2005 at 19:32 UTC | |
by eric256 (Parson) on Mar 25, 2005 at 21:50 UTC |