Return-Path: X-Original-To: apmail-commons-issues-archive@minotaur.apache.org Delivered-To: apmail-commons-issues-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 9B99DD3B6 for ; Wed, 20 Jun 2012 20:45:48 +0000 (UTC) Received: (qmail 42923 invoked by uid 500); 20 Jun 2012 20:45:45 -0000 Delivered-To: apmail-commons-issues-archive@commons.apache.org Received: (qmail 42419 invoked by uid 500); 20 Jun 2012 20:45:44 -0000 Mailing-List: contact issues-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: issues@commons.apache.org Delivered-To: mailing list issues@commons.apache.org Received: (qmail 41343 invoked by uid 99); 20 Jun 2012 20:45:43 -0000 Received: from issues-vm.apache.org (HELO issues-vm) (140.211.11.160) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 20 Jun 2012 20:45:43 +0000 Received: from isssues-vm.apache.org (localhost [127.0.0.1]) by issues-vm (Postfix) with ESMTP id 01D511427F2 for ; Wed, 20 Jun 2012 20:45:43 +0000 (UTC) Date: Wed, 20 Jun 2012 20:45:43 +0000 (UTC) From: "Adrian Nistor (JIRA)" To: issues@commons.apache.org Message-ID: <2047677723.35920.1340225143011.JavaMail.jiratomcat@issues-vm> In-Reply-To: <1582580305.35919.1340225142954.JavaMail.jiratomcat@issues-vm> Subject: [jira] [Updated] (COLLECTIONS-412) CollectionUtils.subtract() is very slow MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 [ https://issues.apache.org/jira/browse/COLLECTIONS-412?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ] Adrian Nistor updated COLLECTIONS-412: -------------------------------------- Attachment: Test.java patch.diff > CollectionUtils.subtract() is very slow > --------------------------------------- > > Key: COLLECTIONS-412 > URL: https://issues.apache.org/jira/browse/COLLECTIONS-412 > Project: Commons Collections > Issue Type: Bug > Environment: java 1.6.0_24 > Ubuntu 11.10 > Reporter: Adrian Nistor > Attachments: Test.java, patch.diff > > > Hi, > I am encountering a performance problem in CollectionUtils.subtract(). > It appears in version 3.2.1 and also in revision 1352300 (20 June > 2012). I attached a test that exposes this problem and a patch that > fixes it. On my machine, for this test, the patch provides a 204X > speedup. > To run the test, just do: > $ java Test > The output for the un-patched version is: > Time is 11036 > The output for the patched version is: > Time is 54 > The root cause of this problem is similar to the root cause of the > previously fixed COLLECTIONS-406 in ListUtils.subtract(), i.e., > quadratic complexity instead of linear complexity. This problem > affects two methods: > CollectionUtils.subtract(final Iterable a, final Iterable b) > and > CollectionUtils.subtract(final Iterable a, final Iterable b, final Predicate p) > because the former just calls the latter. > Currently, the code for > "CollectionUtils.subtract(final Iterable a, final Iterable b, final Predicate p)" > is: > ArrayList list = new ArrayList(); > addAll(list, a); > for (O element : b) { > if (p.evaluate(element)) { > list.remove(element); > } > } > which is quadratic, because "list.remove(element)" has linear > complexity for "ArrayList list = new ArrayList()". > The attached patch makes the remove() be constant complexity by > removing from an org.apache.commons.collections.bag.HashBag. The > attached patch is very similar to the patch of COLLECTIONS-406, so I > assume the risk of applying this patch is minimal. Just like in the > patch for COLLECTIONS-406, this patch uses a HashBag (and not a > HashSet) to respect cardinality when there are repeated objects. > Can you please confirm if the patch is correct? > Thanks, > Adrian -- This message is automatically generated by JIRA. If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa For more information on JIRA, see: http://www.atlassian.com/software/jira