- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Since it compiled after the change, the map can't have been used outside the loop. And with the change, it's now enforced that there are no side-effects involving that variable, making things a little bit clearer for the next person to have to maintain it.
If that change costs 0.0001% performance from just one extra object allocation each time through the loop, it's worth it.
Admin
TRWTF is MM-DD-YY date formatting.
Admin
Admin
I was trying to avoid using words like that in my post... :)
Admin
+1 This seems like premature optimizations. I personally don't reuse objects unless profiling indicates that it's a hotspot. Now depending on the application this code may not be the most efficient in terms of "supposed" GC overhead however if it talks to a database this code is going to be nowhere near as slow as the actual call to a db itself.
Lets think about this, depending on how long the method runs for will basically mean one of a few things, are these in the young gen or has it promoted to O-Gen ... how many hash buckets have been created when the load factor is rehash etc.
So lets assume the default hash buckets of 16 since we don't know what the inner code actually does ... so that's potentially 16 objects dereferenced, the new allocation is only really a small operation and super efficient. Oh also lets not downplay some of the optimizations the JIT could potentially perform such as dead code elimination etc ... really I would have loved to see the bytecode generated here, JIT logs and performance metrics.
Hmm I think Snoofle needs to read Charlie Hunts excellent Java Performance book instead of the usual self righteous BS he usually spouts, sorry Snoofle without more info here I don't agree with that one part of the WTF, for me code should be clear optimize when necessary, simple as. The code also reads like shit so that in and of itself is a WTF ...
As for the other part that made me smile, I love people who use the excuse virtual calls are expensive, you should have seen the mess I saw in some C++ and Java code with this belief.
Admin
TRWTF is that Taran got what he deserved. Most TDWTF stories end with the bad employees forcing the good ones to quit.
Admin
You're using a PC with 4GB of RAM in 2013? With 32-bit Win XP? Why? I currently have 16GB and I was shocked to get an out of memory error recently... so this Christmas means 32GB for me (at least!).
Admin
Actually, the new code may be faster. Creating and GCing a map is O(1), but clearing a map is O(map-size), which can be costly for large maps.
Admin
People say crap like this too often. No, no he wasn't. He was a simpleton who ruined everything, he wasn't bright at all.
Admin
Admin
Also, the GC will eventually run. But if your system does not run out of memory while looping, your GC will at some point in the future have a lot of HashMaps to dispose.
Nothing is for free, but with the HashMap declared outside the loop and only cleared inside it you will have a much more predictable behaviour.
Admin
In other words: if you instantiate the HashMap inside the loop, the end of the loop is the earliest point at which the Garbage Collection is allowed to dispose the HashMap. When it actually is doing so is largely beyond your control, so you could end up with quite a lot of HashMap instances in memory that just take up space while waiting to be garbage collected.
However, clearing a HashMap is something you do have control over. It happens when you call clear().
So, when instantiating the HashMap once outside the loop and then only clearing it inside it, you will get a much more predictable behaviour.
Admin
Is this Java, or did someone just shit a bunch of semicolons and un-intuitive methods all over a screen?
Oh wait...
Admin
What is the cost of throwing away a lot of HashMaps that are not referenced anymore? Nothing, zero. You can always account cost of zeroing to newly created objects.
Pls, get a profiler or heap monitor, write simple (preferably non trivial) piece of code using HashMap, make 1e6 loops in both scenarios. You won't be able to tell the difference.
Admin
Also, I could amend my previous statement: for uniformly distributed numbers that are NOT arbitrarily large, but which ARE evenly distributed from base^i (or zero) to base^j - 1 for any positive integers i and j, THEN the distribution of digits in the greatest significance place will also be uniform.
Admin
It seems 'fairly bright' persons can also suffer from the Dunning-Kruger effect.
Captch: commoveo - that did not commove me
Admin
Admin
Admin
Admin
p.s. and then I'll define a set that includes all real numbers that aren't included in your set, so that the leading digits of all the numbers will be evenly distributed again (LIKE THEY'RE SUPPOSED TO BE).
Admin
PS: https://en.wikipedia.org/wiki/Benford%27s_law
Admin
Oh, yes it does have to do with patterns, namely anti-patterns.
Admin
Doing nothing with the Map: reusing the Map is about 6x times faster (6 vs 36 ms).
Reuse Map and add 100 elements on each iteration: 5.1 s Create new Map and add 100 elements on each iteration: 6.7 s
Reuse Map and add 1000 elements on each iteration: 62 s Create new Map and add 1000 elements on each iteration: 81 s
In both cases reusing the Map is about 23% faster.
Admin
Obviously!
Admin
Obviously.
Admin
The predictable behavior being that your hashMap lost references to the objects it knew about.
That does not guarantee a release in memory of the objects the hashMap referred to, even if the hashMap was the only reference to those objects. You're still dealing with the GC releasing those objects whenever it feels like.
Admin
Admin
As for migrating the OS, I'm getting used to Windows 7 on another PC so that will be a possibility some day. It will take more time to migrate applications than to migrate the OS.
Admin
Adding more whitespace in their code will speed it up significantly:
Admin
Admin
He's using the word obviously too often to excuse not saying whatever the other person said before he got to it.
Admin
Try adding 1000 elements first, then do 100 each iteration. Clearing will be slower because it has to clear 1000 element map regardless of how many are actually in it
Admin
No - Obviously.
I have personally seen patterns implemented poorly - singletons that were not thread safe, factories that were simply a facade (wrappers around creating the object - a separate method for each type of object) and totally unneeded and on and on.
The only pattern usage I saw in the examples for this WTF were patterns of an total lack of understanding of the very basic fundamentals of logic.
Unfortunately I have seen way too many examples of a lack of understanding the fundamentals too; e.g.:
HashMap someTuple = new HashMap(5);
To hold five items. If why this is flawed isn't obvious to someone, then they should go lookup how hash maps work.
Admin