|
What is the best way to optimize the following bit of code in Java 8?code:
Carbon dioxide fucked around with this message at 18:45 on Mar 3, 2016 |
# ? Mar 3, 2016 18:43 |
|
|
# ? Jun 13, 2024 04:36 |
|
code:
|
# ? Mar 3, 2016 18:56 |
|
You probably want allowedIds to be a Set
|
# ? Mar 3, 2016 19:06 |
|
Hm, if you're really sure selectedFooIDs is the smaller of the two, then that's pretty good already. I agree with Sedro that you should make allowedIDs a set because they're unique, no need to have duplicates or check for duplicates. Not a huge optimization if the lists don't have duplicates but if they do it helps, and it doesn't hurt if they don't. Well, you could use a hashset to make it faster so you don't have to iterate the set when you call contains(). https://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html code:
Illegal Move posted:
Is this actually more efficient or is it just shorter code? I feel like its gotta be doing the same O(n^2) operation either way. Zaphod42 fucked around with this message at 19:25 on Mar 3, 2016 |
# ? Mar 3, 2016 19:16 |
|
Zaphod42 posted:Is this actually more efficient or is it just shorter code? I feel like its gotta be doing the same O(n^2) operation either way. It's not more efficient (as far as I know), I just assumed that he meant shorter code because he said "Java 8", but now that I've reread his post, I think I may have been wrong!
|
# ? Mar 3, 2016 19:32 |
|
Both of those look right to me depending on what you want to optimize . Is that code particularly slow or do you just imagine it might be?
|
# ? Mar 3, 2016 19:35 |
|
Wouldn't it be even more fun to doJava code:
I just like all the neat collections wrangling functions
|
# ? Mar 3, 2016 19:36 |
|
baka kaba posted:Wouldn't it be even more fun to do Oh, for some reason I thought he was calling doSomethingImportant(Foo) on the foo objects, but if that just needs the long ID then yeah, that works great. And even if it doesn't you could probably put them in a HashMap<Long, Foo> and then use the keyset with retainAll() and then pull the relevant entries from the HashMap...
|
# ? Mar 3, 2016 19:42 |
|
Streams have some API magic going on where they parallel process things as much as possible. A stream method doesn't need to wait for the previous method to have finished. So if you have Stream.filter(whatever).map(whatever) it will somehow combine those methods to give a really fast result. I guess that doesn't really matter in this case, but I'm trying to get the hang of Java 8, and I thought this bit of code I was dealing with could be replaced by some smart use of Streams. Using a HashSet is also a good idea, though.
|
# ? Mar 3, 2016 19:50 |
|
Zaphod42 posted:Hm, if you're really sure selectedFooIDs is the smaller of the two, then that's pretty good already. I agree with Sedro that you should make allowedIDs a set because they're unique, no need to have duplicates or check for duplicates. Not a huge optimization if the lists don't have duplicates but if they do it helps, and it doesn't hurt if they don't. baka kaba posted:Wouldn't it be even more fun to do Sedro fucked around with this message at 19:56 on Mar 3, 2016 |
# ? Mar 3, 2016 19:52 |
|
I suppose HashSet.retainAll() is also faster than List.retainAll()?
|
# ? Mar 3, 2016 19:56 |
|
Carbon dioxide posted:What is the best way to optimize the following bit of code in Java 8? Depending on what doSomethingImportant(id) is, you could also use parallelStream() to do something like this: Java code:
E: And yeah, use sets if possible. They should be faster for non-trivial sizes. Loezi fucked around with this message at 20:12 on Mar 3, 2016 |
# ? Mar 3, 2016 20:05 |
|
Sedro posted:You've changed the behavior because you will doSomethingImportant in an arbitrary order now That's true, if it's important! You could copy the list and do a retainAll on that, if one-off filtering is more efficient I should have spotted that though, I was blinded by the Collections goodness
|
# ? Mar 3, 2016 20:17 |
|
Doing it in parallel is technically the exact same big-O runtime and amount of instructions executed. Its equally as efficient, although on a multi-core processor it would be 'faster' yeah. Depends upon what you mean by optimize. In general though, don't forget: Pre-mature optimization is the devil! Don't worry about efficiency too much (beyond "don't do really stupid wasteful operations if there's a clear alternative) unless you're sure this is a bottleneck. http://c2.com/cgi/wiki?PrematureOptimization E: Also doing it parallel means that method you're calling has to be thread-safe now. Zaphod42 fucked around with this message at 21:15 on Mar 3, 2016 |
# ? Mar 3, 2016 20:22 |
|
Carbon dioxide posted:I suppose HashSet.retainAll() is also faster than List.retainAll()? They both basically iterate over their contents and call contains(element) on the collection you pass in to retain from, so I'd expect the type of passed collection to matter more. ArrayList.contains() iterates over every element until it finds one that matches, HashSet.contains() uses hashing for faster access. Benchmarking would be the way to go though, if it's important!
|
# ? Mar 3, 2016 20:29 |
|
baka kaba posted:They both basically iterate over their contents and call contains(element) on the collection you pass in to retain from, so I'd expect the type of passed collection to matter more. ArrayList.contains() iterates over every element until it finds one that matches, HashSet.contains() uses hashing for faster access. Benchmarking would be the way to go though, if it's important! To give a bit more in depth explanation, the operation originally as originally written is the intersection of two sets. As noted by baka kaba, the retainAll in the case of a general list structure is an n^2 operation. However, by using a set optimized structure like a HashSet, the operation becomes an n operation. If order is a concern, consider a TreeSet which provides in order operation.
|
# ? Mar 4, 2016 07:50 |
|
If order is not important it might also be faster to create a set of the selected ids instead and loop over the allowed ids.code:
|
# ? Mar 4, 2016 19:19 |
|
Why the heck does this keep giving me a "OutOfMemoryError: Java heap space" error? Reading stuff online doesn't really help.code:
EDIT: I'm an idiot. You can all laugh at me. It's because I used while instead of an if.
|
# ? Mar 6, 2016 23:37 |
|
Java has immutable strings and the variable isnt getting reassigned, take a second to think about this:code:
|
# ? Mar 6, 2016 23:58 |
|
Now I'm laughing at you, instead!
|
# ? Mar 7, 2016 00:03 |
|
Why? Was the problem unclear?
|
# ? Mar 7, 2016 01:16 |
|
Casual poster had already edited in the answer by the time you replied.
|
# ? Mar 7, 2016 07:30 |
|
I appreciated the response anyways! What I don't get was why this was different than before. In the past I would of just created an infinite loop but this time I ran out of memory?
|
# ? Mar 7, 2016 11:03 |
|
casual poster posted:I appreciated the response anyways! It keeps adding the string to z, which grows infinitely large, causing you to run out of memory. Your previous loops probably just caused int overflows or something like it, which doesn't stop execution.
|
# ? Mar 7, 2016 11:10 |
|
ah ok, that makes sense. Thanks.
|
# ? Mar 7, 2016 11:56 |
|
If a class is defined inside a method, will it waste time on runtime defining the class every time the method is run?
|
# ? Mar 11, 2016 11:28 |
|
Not much more overhead than regular object allocation. I'd worry more about the implicit strong reference to the outer class that the inner class keeps, and potentially leaking stuff that way.
speng31b fucked around with this message at 14:20 on Mar 11, 2016 |
# ? Mar 11, 2016 14:04 |
|
Boz0r posted:If a class is defined inside a method, will it waste time on runtime defining the class every time the method is run? To clarify, the compiler will generate the class definition at compile time, along with synthetic accessors etc. If you take a look at the generated classes, you'll find something like YourClass$1, YourClass$2, etc for the anonymous inner methods of your class. So, you'll pay the instancing penalty of any object, but otherwise there's no real cost.
|
# ? Mar 11, 2016 14:28 |
|
Is the Eclipse refactoring tool worth using on a small project? About a thousand lines total. I figure if I'm going to use JUnit, I may as well run the refactoring so I have something to automatically test.
|
# ? Mar 12, 2016 01:20 |
|
What refactoring are you looking to do? I mean, as long as you have a snapshot from before you run the tool it can't hurt to try on a small project.
|
# ? Mar 12, 2016 01:30 |
|
Learning how to use refactoring tools is an important part of an effective workflow so yeah. Like the above post mentioned, make sure your project is in a good state with version control before you go nuts.
|
# ? Mar 12, 2016 01:50 |
|
Yep, I've got a git repository set up for the project. I can just roll it back if I need to. Does anyone have a good source for learning JUnit? The tutorials I've found don't really explain anything, just give you stuff to copy and paste. 22 Eargesplitten fucked around with this message at 02:27 on Mar 12, 2016 |
# ? Mar 12, 2016 02:22 |
|
22 Eargesplitten posted:Does anyone have a good source for learning JUnit? The tutorials I've found don't really explain anything, just give you stuff to copy and paste. What are you having trouble learning in JUnit?
|
# ? Mar 12, 2016 04:38 |
|
This one looks decent (assuming you're using JUnit4) https://www.javacodegeeks.com/2014/11/junit-tutorial-unit-testing.html
|
# ? Mar 12, 2016 05:20 |
|
Maybe something like Test Driven Development by Example would be useful? http://www.amazon.com/Test-Driven-Development-By-Example/dp/0321146530 JUnit as such is a pretty shallow subject in my opinion, but writing good tests or using mocking is where the complexity comes in.
|
# ? Mar 12, 2016 05:57 |
|
22 Eargesplitten posted:Is the Eclipse refactoring tool worth using on a small project? About a thousand lines total. I figure if I'm going to use JUnit, I may as well run the refactoring so I have something to automatically test. Refactoring should be a regular use of every developer, as should JUnit. My development process is RGR (Red Green Refactor ) (typical TDD development BTW) You write a failing test (RED) You write code to make the test Pass (GREEN) you then refactor the code to make it not suck so much (REFACTOR) Rinse and repeat until software released
|
# ? Mar 12, 2016 06:00 |
|
22 Eargesplitten posted:Yep, I've got a git repository set up for the project. I can just roll it back if I need to. Unit testing can take a little while to "get" but once you understand the process its easy. One big mistake lots of people make is to do the Partial Mock . Stay clear of that, a Class has a TestClass and in that test class the one main rule is NEVER EVER EVER Mock any part of the Class you are testing. As for TDD examples try the bowling Kata http://butunclebob.com/files/downloads/Bowling%20Game%20Kata.ppt
|
# ? Mar 12, 2016 06:03 |
|
TheresaJayne posted:Stay clear of that, a Class has a TestClass and in that test class the one main rule is NEVER EVER EVER Mock any part of the Class you are testing. Gah, I've wasted so much time writing tests that don't do anything besides ensure that my mocks do what I think they do. :P
|
# ? Mar 12, 2016 06:12 |
|
baquerd posted:What are you having trouble learning in JUnit? How to set up the test suite and test case classes. I feel like once I've got the basic structure up and running, I can figure everything else out. I get that the idea is to test that input of x to function foo causes an output of y.
|
# ? Mar 12, 2016 06:22 |
|
|
# ? Jun 13, 2024 04:36 |
|
Bear in mind that TDD is not for everyone. Code first, tests later is also perfectly valid.
|
# ? Mar 12, 2016 06:28 |