Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #24923
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Cost of creating objects? |
| Date | 2013-08-07 20:51 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <ktuq3e$amp$1@dont-email.me> (permalink) |
| References | <ktstsp$59k$1@news.albasani.net> <code-review-20130807173646@ram.dialup.fu-berlin.de> <ktu1eu$dbt$1@news.albasani.net> <ktu8h4$ftf$1@dont-email.me> <d0ae8f7d-89a1-44f8-ac8c-8c7e9038dc1e@googlegroups.com> |
On 8/7/2013 7:52 PM, Lew wrote:
> Eric Sosman wrote:
>> Sebastian wrote:
>>> [...] The reason I asked is we have a person in our group
>>> who is very much in a micro-optimization mind-set. Among the things
>>> that concern him are, for example, temporary local variables, wrappers
>>> as method arguments wasting heap space compared to primitives,
>>> multiple hash map lookups (containsKey() followed by get()) etc.
>
> Overrule him. He's an idiot. Although doing a 'containsKey()' before a 'get()' is silly.
Situation: A Map that is searched for keys that are usually
*not* present (poor man's Bloom filter, say), and some of whose
mapped values are null. Now, attend:
if (map.containsKey(key)) { // usually false
Value val = map.get(key); // seldom needed
...
}
vs.
Value val = map.get(key);
if (val == null // usually true
|| map.containsKey(key) // usually false
) {
...
}
If the key is present 0.0 <= p <= 1.0 of the time, the first
version does (1 + p) searches, the second (1 + (1 - p)) = (2 - p).
Even if the second search is cheaper than the first (maybe the
key's hashCode has been cached), for small p the first wins.
--
Eric Sosman
esosman@comcast-dot-net.invalid
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Cost of creating objects? Sebastian <news@seyweiler.dyndns.org> - 2013-08-07 09:44 +0200
Re: Cost of creating objects? Donkey Hottie <donkey@fredriksson.dy.fi> - 2013-08-07 13:02 +0300
Re: Cost of creating objects? Sebastian <news@seyweiler.dyndns.org> - 2013-08-07 12:05 +0200
Re: Cost of creating objects? Donkey Hottie <donkey@fredriksson.dy.fi> - 2013-08-07 14:08 +0300
Re: Cost of creating objects? Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-08-07 08:08 -0400
Re: Cost of creating objects? Kevin McMurtrie <mcmurtrie@pixelmemory.us> - 2013-08-07 20:14 -0700
Re: Cost of creating objects? Arved Sandstrom <asandstrom2@eastlink.ca> - 2013-08-08 17:17 -0300
Re: Cost of creating objects? Joerg Meier <joergmmeier@arcor.de> - 2013-08-07 12:37 +0200
Re: Cost of creating objects? Sebastian <news@seyweiler.dyndns.org> - 2013-08-07 19:51 +0200
Re: Cost of creating objects? Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-08-07 15:51 -0400
Re: Cost of creating objects? Lew <lewbloch@gmail.com> - 2013-08-07 16:52 -0700
Re: Cost of creating objects? Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-08-07 20:51 -0400
Re: Cost of creating objects? Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2013-08-08 11:24 +0300
Re: Cost of creating objects? lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-08-08 10:07 +0100
Re: Cost of creating objects? Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2013-08-08 17:34 +0300
Re: Cost of creating objects? Arne Vajhøj <arne@vajhoej.dk> - 2013-08-08 10:42 -0400
Re: Cost of creating objects? lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-08-08 20:45 +0100
Re: Cost of creating objects? Arne Vajhøj <arne@vajhoej.dk> - 2013-08-08 10:38 -0400
Re: Cost of creating objects? Eric Sosman <esosman@comcast-dot-net.invalid> - 2013-08-08 10:13 -0400
Re: Cost of creating objects? Arne Vajhøj <arne@vajhoej.dk> - 2013-08-07 20:28 -0400
Re: Cost of creating objects? Arved Sandstrom <asandstrom2@eastlink.ca> - 2013-08-08 17:34 -0300
Re: Cost of creating objects? lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-08-07 19:19 +0100
Re: Cost of creating objects? lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-08-07 19:24 +0100
Re: Cost of creating objects? Kevin McMurtrie <mcmurtrie@pixelmemory.us> - 2013-08-07 20:03 -0700
Re: Cost of creating objects? Arved Sandstrom <asandstrom2@eastlink.ca> - 2013-08-08 17:39 -0300
Re: Cost of creating objects? Arne Vajhøj <arne@vajhoej.dk> - 2013-08-07 20:24 -0400
csiph-web