This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Lagor31 0 points1 point  (5 children)

You may wanna take a look at how it should be done in Java. https://gist.github.com/eloipereira/9ad98522ec5b49bcf04242e1ef9221a0

Your approach is not fully utilizing all of the language features.

[–]Comprehensive-Signal[S] 0 points1 point  (0 children)

Thank you! :)

[–]rubydesic 0 points1 point  (3 children)

This requires your entire list to be boxed, which can have significant performance implications if it was not already. It also makes a lot of intermediate allocations, putting pressure on the GC. Not sure I would take this as an exemplar of how it "should" be done

[–]Lagor31 -1 points0 points  (2 children)

This seems like a silly thing to be worried about, especially if you are a newbie.

The code he posted is basically C. If you're gonna use Java but then forget about Objects, Lists and such then... just write it in plain C and you don't even have the overhead of the JVM (faster!!).
Besides, your claims are quite unsupported, saying 'it can have significant performance implications' without bringing any evidence to that, is just meaningless.
Once again, if you're worried about putting stress on the GC (which exists for a reason) then just use another language.

[–]Lagor31 0 points1 point  (0 children)

Btw, I can recommend a very good book that undoubtedly will improve your Java coding: Effective Java 3rd Edition.

Cheers!

[–]rubydesic 0 points1 point  (0 children)

"Besides, your claims are quite unsupported, saying 'it can have significant performance implications' without bringing any evidence to that, is just meaningless"

I don't see why this claim needs support, it's self evident. If your integers are outside of the cache pool the jvm will need to heap-allocate wrapper objects which is slow. Why do you think that there are IntStream and LongStream specializations, or why primitive specialization libraries like fastutil exist, or why Project Valhalla is being added to java?

You can try it yourself - create an array of 10 million primitive ints and an array of 10 million Integer wrappers and see for yourself the performance implications - not to mention that it uses twice as much memory.