![]() |
VOOZH | about |
Lead developer at Electrolux Group
Tokyo, JP
Joined May 2011
Leo Leo is a Java Geek and had the opportunity to apply his skills to various industry, from aerospace, e-commerce, and gaming.
Stats
| Reputation: | 3 |
| Pageviews: | 269.7K |
| Articles: | 2 |
| Comments: | 18 |
Comments
Jul 20, 2011 · James Sugrue
Mariusz,
Yes indeed, the equals() and hashCode() methods ot the contained objects will have an impact in the absolute results measured, especially this impact will be visible with a small number of elements in the collection.
But in the end, these two methods have a O(1) complexity, so for collection with let's say 10000+ elements, their impact will be negligeable compared to a loop on a List when searching for an element to remove for example. That's why in such case the HashSet will always have better performances than an ArrayList.
But please feal free to download and modify the benchmark code here.
You can pretty easily replace the String by some more complex object (regarding its equals/hashCode method) and see untill what population size these methods have a visible impact on the relative performances between Set and List for example.
Leo
Jul 20, 2011 · James Sugrue
Mariusz,
Yes indeed, the equals() and hashCode() methods ot the contained objects will have an impact in the absolute results measured, especially this impact will be visible with a small number of elements in the collection.
But in the end, these two methods have a O(1) complexity, so for collection with let's say 10000+ elements, their impact will be negligeable compared to a loop on a List when searching for an element to remove for example. That's why in such case the HashSet will always have better performances than an ArrayList.
But please feal free to download and modify the benchmark code here.
You can pretty easily replace the String by some more complex object (regarding its equals/hashCode method) and see untill what population size these methods have a visible impact on the relative performances between Set and List for example.
Leo
Jul 20, 2011 · James Sugrue
Mariusz,
Yes indeed, the equals() and hashCode() methods ot the contained objects will have an impact in the absolute results measured, especially this impact will be visible with a small number of elements in the collection.
But in the end, these two methods have a O(1) complexity, so for collection with let's say 10000+ elements, their impact will be negligeable compared to a loop on a List when searching for an element to remove for example. That's why in such case the HashSet will always have better performances than an ArrayList.
But please feal free to download and modify the benchmark code here.
You can pretty easily replace the String by some more complex object (regarding its equals/hashCode method) and see untill what population size these methods have a visible impact on the relative performances between Set and List for example.
Leo
Jul 20, 2011 · James Sugrue
Mariusz,
Yes indeed, the equals() and hashCode() methods ot the contained objects will have an impact in the absolute results measured, especially this impact will be visible with a small number of elements in the collection.
But in the end, these two methods have a O(1) complexity, so for collection with let's say 10000+ elements, their impact will be negligeable compared to a loop on a List when searching for an element to remove for example. That's why in such case the HashSet will always have better performances than an ArrayList.
But please feal free to download and modify the benchmark code here.
You can pretty easily replace the String by some more complex object (regarding its equals/hashCode method) and see untill what population size these methods have a visible impact on the relative performances between Set and List for example.
Leo
Jul 18, 2011 · James Sugrue
Artur,
Yes you are absolutely right. This combined list has poor performances with interleaved operations due to the re-population of internal collections.
Well, in fact, I first implemented this CombinedList for the model of a JTable.
In my case, operations were quite done in batch, add a lot of data, then add another set of data but before adding filter data that are already in the list (with the contains), then remove a range of data, set a range of Data, etc. And for this case it was a good compromise.
But for interleaved operations, another collection is a better choice indeed.
Jul 18, 2011 · James Sugrue
Artur,
Yes you are absolutely right. This combined list has poor performances with interleaved operations due to the re-population of internal collections.
Well, in fact, I first implemented this CombinedList for the model of a JTable.
In my case, operations were quite done in batch, add a lot of data, then add another set of data but before adding filter data that are already in the list (with the contains), then remove a range of data, set a range of Data, etc. And for this case it was a good compromise.
But for interleaved operations, another collection is a better choice indeed.
Jul 18, 2011 · James Sugrue
Artur,
Yes you are absolutely right. This combined list has poor performances with interleaved operations due to the re-population of internal collections.
Well, in fact, I first implemented this CombinedList for the model of a JTable.
In my case, operations were quite done in batch, add a lot of data, then add another set of data but before adding filter data that are already in the list (with the contains), then remove a range of data, set a range of Data, etc. And for this case it was a good compromise.
But for interleaved operations, another collection is a better choice indeed.
Jul 18, 2011 · James Sugrue
Artur,
Yes you are absolutely right. This combined list has poor performances with interleaved operations due to the re-population of internal collections.
Well, in fact, I first implemented this CombinedList for the model of a JTable.
In my case, operations were quite done in batch, add a lot of data, then add another set of data but before adding filter data that are already in the list (with the contains), then remove a range of data, set a range of Data, etc. And for this case it was a good compromise.
But for interleaved operations, another collection is a better choice indeed.
Jul 18, 2011 · James Sugrue
Thomas,
Very interesting articles about micro benchmark.
Indeed you are absolutely right about the fact that the use of nanoTime() is not really suitable here, and the fact that this benchmark might not be absolutely corrects, because of several factors that I have ignored.
But in fact, it's not really a big matter in my humble opinion. As I mentioned it, my purpose is not to have a complete benchmark, but just a global idea of collection performances. And that's why I used a timeout, and also why I presented results in charts, to compare them relatively, but not in some table, because the absolute value does not really interest me here.
It's very difficult to implement a good benchmark as it is said in the articles that you pointed, and for this reason it's sometimes difficult to find some benchmark results on the Internet, which is a shame in my opinion.
So here is an incomplete/imperfect/simplistic benchmark of Java collections, but which I hope give some global idea on this topic.
Jul 18, 2011 · James Sugrue
Thomas,
Very interesting articles about micro benchmark.
Indeed you are absolutely right about the fact that the use of nanoTime() is not really suitable here, and the fact that this benchmark might not be absolutely corrects, because of several factors that I have ignored.
But in fact, it's not really a big matter in my humble opinion. As I mentioned it, my purpose is not to have a complete benchmark, but just a global idea of collection performances. And that's why I used a timeout, and also why I presented results in charts, to compare them relatively, but not in some table, because the absolute value does not really interest me here.
It's very difficult to implement a good benchmark as it is said in the articles that you pointed, and for this reason it's sometimes difficult to find some benchmark results on the Internet, which is a shame in my opinion.
So here is an incomplete/imperfect/simplistic benchmark of Java collections, but which I hope give some global idea on this topic.
Jul 18, 2011 · James Sugrue
Thomas,
Very interesting articles about micro benchmark.
Indeed you are absolutely right about the fact that the use of nanoTime() is not really suitable here, and the fact that this benchmark might not be absolutely corrects, because of several factors that I have ignored.
But in fact, it's not really a big matter in my humble opinion. As I mentioned it, my purpose is not to have a complete benchmark, but just a global idea of collection performances. And that's why I used a timeout, and also why I presented results in charts, to compare them relatively, but not in some table, because the absolute value does not really interest me here.
It's very difficult to implement a good benchmark as it is said in the articles that you pointed, and for this reason it's sometimes difficult to find some benchmark results on the Internet, which is a shame in my opinion.
So here is an incomplete/imperfect/simplistic benchmark of Java collections, but which I hope give some global idea on this topic.
Jul 18, 2011 · James Sugrue
Thomas,
Very interesting articles about micro benchmark.
Indeed you are absolutely right about the fact that the use of nanoTime() is not really suitable here, and the fact that this benchmark might not be absolutely corrects, because of several factors that I have ignored.
But in fact, it's not really a big matter in my humble opinion. As I mentioned it, my purpose is not to have a complete benchmark, but just a global idea of collection performances. And that's why I used a timeout, and also why I presented results in charts, to compare them relatively, but not in some table, because the absolute value does not really interest me here.
It's very difficult to implement a good benchmark as it is said in the articles that you pointed, and for this reason it's sometimes difficult to find some benchmark results on the Internet, which is a shame in my opinion.
So here is an incomplete/imperfect/simplistic benchmark of Java collections, but which I hope give some global idea on this topic.
Jul 07, 2011 · Leo Lewis
Anant,
Interesting question you have here.
Yes, you can command a JVM1 to restart from another JVM2 (or another process, not necessarily a JVM), by sending some signal to the JVM1 which will process the signal and call the restartApplication method.
We can think of sending this reboot signal by socket, or HTTP etc.
As the launch of the new JVM is done in a shutdownHook, you should not have some trouble of port already used by some SocketServer.
I made this experience with my Tomcat, I command it to restart itself from my web browser by calling this simple servlet (forgive me the fact that usually the SecurityManager should not permit a System.exit() to be called by a servlet).
public class RestartTomcatServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String query = req.getParameter("query"); if ("restart".equals(query)) { restartApplication(new Runnable() { @Override public void run() { System.out.println("Restarting the server ..."); } }); } else { resp.getWriter().println("Server uptime : " + (ManagementFactory.getRuntimeMXBean().getUptime() / 1000) + "s"); } } }
Start tomcat, wait a little bit, call my servlet with no argument :
Server uptime : 57s
Calling my servlet with ?query=restart argument :
Page is loading, refresh the page after a couple of seconds :
Server uptime : 8s
Leo
Jul 07, 2011 · Leo Lewis
Anant,
Interesting question you have here.
Yes, you can command a JVM1 to restart from another JVM2 (or another process, not necessarily a JVM), by sending some signal to the JVM1 which will process the signal and call the restartApplication method.
We can think of sending this reboot signal by socket, or HTTP etc.
As the launch of the new JVM is done in a shutdownHook, you should not have some trouble of port already used by some SocketServer.
I made this experience with my Tomcat, I command it to restart itself from my web browser by calling this simple servlet (forgive me the fact that usually the SecurityManager should not permit a System.exit() to be called by a servlet).
public class RestartTomcatServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String query = req.getParameter("query"); if ("restart".equals(query)) { restartApplication(new Runnable() { @Override public void run() { System.out.println("Restarting the server ..."); } }); } else { resp.getWriter().println("Server uptime : " + (ManagementFactory.getRuntimeMXBean().getUptime() / 1000) + "s"); } } }
Start tomcat, wait a little bit, call my servlet with no argument :
Server uptime : 57s
Calling my servlet with ?query=restart argument :
Page is loading, refresh the page after a couple of seconds :
Server uptime : 8s
Leo
Jul 06, 2011 · Leo Lewis
Thank you Dmirty,
In fact the quotes are probably not needed on most linux/unix installation, because the path to the java executable usually does not contain spaces " " characters (something like /usr/java/jdk.../bin/java).
But it is usually required on Windows where java is installed by default in C:\Program files\Java\jdk...\bin\java.
In this case, the command will failed if you miss the quotes.
Leo
Jul 06, 2011 · Leo Lewis
Thank you Dmirty,
In fact the quotes are probably not needed on most linux/unix installation, because the path to the java executable usually does not contain spaces " " characters (something like /usr/java/jdk.../bin/java).
But it is usually required on Windows where java is installed by default in C:\Program files\Java\jdk...\bin\java.
In this case, the command will failed if you miss the quotes.
Leo
Jul 06, 2011 · Leo Lewis
Thank you Andreas,
Indeed, you are totally right, the sun.java.command is the weak point of this method. I updated the article to set it constant in my code, and add some comments about it. It's a shame because it does exactly what I was looking for.
A workaround which would avoid to use it, would be to create a class RestartManager that would contain a singleton, our restartApplication() method and some method :
public void registerMain(Class<?> main, String[] args) { ... }
that would be called in the main method of the application, to keep somewhere in memory the main class and the launch arguments. We lost the simplicity of a single static method that do the all trick, but we gain in portability.
Regards,
Leo
Jul 06, 2011 · Leo Lewis
Thank you Andreas,
Indeed, you are totally right, the sun.java.command is the weak point of this method. I updated the article to set it constant in my code, and add some comments about it. It's a shame because it does exactly what I was looking for.
A workaround which would avoid to use it, would be to create a class RestartManager that would contain a singleton, our restartApplication() method and some method :
public void registerMain(Class<?> main, String[] args) { ... }
that would be called in the main method of the application, to keep somewhere in memory the main class and the launch arguments. We lost the simplicity of a single static method that do the all trick, but we gain in portability.
Regards,
Leo
User has been successfully modified
Failed to modify user
ADVERTISE
CONTRIBUTE ON DZONE
LEGAL
CONTACT US
Let's be friends: