[ 
https://issues.apache.org/jira/browse/IO-670?focusedWorklogId=464127&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-464127
 ]

ASF GitHub Bot logged work on IO-670:
-------------------------------------

                Author: ASF GitHub Bot
            Created on: 29/Jul/20 17:38
            Start Date: 29/Jul/20 17:38
    Worklog Time Spent: 10m 
      Work Description: eolivelli commented on a change in pull request #118:
URL: https://github.com/apache/commons-io/pull/118#discussion_r462154657



##########
File path: 
src/test/java/org/apache/commons/io/performance/IOUtilsContentEqualsPerformanceTest.java
##########
@@ -0,0 +1,178 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.io.performance;
+
+import org.apache.commons.io.IOUtils;
+import org.openjdk.jmh.annotations.*;
+
+import java.io.*;
+import java.util.concurrent.TimeUnit;
+
+import static org.apache.commons.io.IOUtils.EOF;
+import static org.apache.commons.io.IOUtils.toBufferedReader;
+
+/**
+ * Test to show whether using BitSet for removeAll() methods is faster than 
using HashSet.
+ */
+@BenchmarkMode(Mode.AverageTime)
+@OutputTimeUnit(TimeUnit.NANOSECONDS)
+@State(Scope.Thread)
+public class IOUtilsContentEqualsPerformanceTest {
+
+    private static final String[] STRINGS = new String[3];
+
+    static {
+        STRINGS[0] = getString0();
+        STRINGS[1] = STRINGS[0] + 'c';
+        STRINGS[2] = STRINGS[0] + 'd';
+    }
+
+    private static final int LOOP = 10;
+
+    @Benchmark
+    public boolean[] testContentEqualsForFileNew() throws IOException {
+        boolean[] res = new boolean[2];
+        for (int i = 0; i < LOOP; i++) {
+            try (InputStream inputStream1 =
+                         
this.getClass().getResourceAsStream("/org/apache/commons/io/testfileBOM.xml");
+                 InputStream inputStream2 =
+                         
this.getClass().getResourceAsStream("/org/apache/commons/io/testfileNoBOM.xml");
+                 Reader inputReader1 = new InputStreamReader(inputStream1);
+                 Reader inputReader2 = new InputStreamReader(inputStream2);
+            ) {
+                res[0] = IOUtils.contentEquals(inputReader1, inputReader2);
+            }
+            try (InputStream inputStream1 =
+                         
this.getClass().getResourceAsStream("/org/apache/commons/io/testfileBOM.xml");
+                 InputStream inputStream2 =
+                         
this.getClass().getResourceAsStream("/org/apache/commons/io/testfileBOM.xml");
+                 Reader inputReader1 = new InputStreamReader(inputStream1);
+                 Reader inputReader2 = new InputStreamReader(inputStream2);
+            ) {
+                res[1] = IOUtils.contentEquals(inputReader1, inputReader2);
+            }
+        }
+        return res;
+    }
+
+    @Benchmark
+    public boolean[] testContentEqualsOld() throws IOException {
+        boolean[] res = new boolean[2];
+        for (int i = 0; i < LOOP; i++) {
+            try (InputStream inputStream1 =
+                         
this.getClass().getResourceAsStream("/org/apache/commons/io/testfileBOM.xml");
+                 InputStream inputStream2 =
+                         
this.getClass().getResourceAsStream("/org/apache/commons/io/testfileNoBOM.xml");
+                 Reader inputReader1 = new InputStreamReader(inputStream1);
+                 Reader inputReader2 = new InputStreamReader(inputStream2);
+            ) {
+                res[0] = contentEqualsOld(inputReader1, inputReader2);
+            }
+            try (InputStream inputStream1 =
+                         
this.getClass().getResourceAsStream("/org/apache/commons/io/testfileBOM.xml");
+                 InputStream inputStream2 =
+                         
this.getClass().getResourceAsStream("/org/apache/commons/io/testfileBOM.xml");
+                 Reader inputReader1 = new InputStreamReader(inputStream1);
+                 Reader inputReader2 = new InputStreamReader(inputStream2);
+            ) {
+                res[1] = contentEqualsOld(inputReader1, inputReader2);
+            }
+        }
+        return res;
+    }
+
+    @Benchmark
+    public boolean[] testContentEqualsNew2() throws IOException {
+        boolean[] res = new boolean[9];
+        for (int i = 0; i < 3; i++) {
+            for (int j = 0; j < 3; j++) {
+                try (Reader inputReader1 = new StringReader(STRINGS[i]);
+                     Reader inputReader2 = new StringReader(STRINGS[j]);
+                ) {
+                    res[i * 3 + j] = IOUtils.contentEquals(inputReader1, 
inputReader2);
+                }
+            }
+        }
+        return res;
+    }
+
+    @Benchmark
+    public boolean[] testContentEqualsOld2() throws IOException {
+        boolean[] res = new boolean[9];
+        for (int i = 0; i < 3; i++) {
+            for (int j = 0; j < 3; j++) {
+                try (Reader inputReader1 = new StringReader(STRINGS[i]);
+                     Reader inputReader2 = new StringReader(STRINGS[j]);
+                ) {
+                    res[i * 3 + j] = contentEqualsOld(inputReader1, 
inputReader2);
+                }
+            }
+        }
+        return res;
+    }
+
+    /**
+     * Old version of IOUtils.contentEquals(Reader, Reader)
+     *
+     * Compares the contents of two Readers to determine if they are equal or
+     * not.
+     * <p>
+     * This method buffers the input internally using
+     * <code>BufferedReader</code> if they are not already buffered.
+     * </p>
+     *
+     * @param input1 the first reader
+     * @param input2 the second reader
+     * @return true if the content of the readers are equal or they both don't
+     * exist, false otherwise
+     * @throws NullPointerException if either input is null
+     * @throws IOException          if an I/O error occurs
+     * @since 1.1
+     */
+    @SuppressWarnings("resource")
+    public static boolean contentEqualsOld(final Reader input1, final Reader 
input2)
+            throws IOException {
+        if (input1 == input2) {
+            return true;
+        }
+        if (input1 == null ^ input2 == null) {
+            return false;
+        }
+        final BufferedReader bufferedInput1 = toBufferedReader(input1);
+        final BufferedReader bufferedInput2 = toBufferedReader(input2);
+
+        int ch = bufferedInput1.read();
+        while (EOF != ch) {
+            final int ch2 = bufferedInput2.read();
+            if (ch != ch2) {
+                return false;
+            }
+            ch = bufferedInput1.read();
+        }
+
+        return bufferedInput2.read() == EOF;
+    }
+
+    public static String getString0() {
+        StringBuilder stringBuilder = new StringBuilder("ab");
+        for (int i = 0; i < 24; i++) {

Review comment:
       Got it.
   Sorry for the confusion 




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Issue Time Tracking
-------------------

    Worklog Id:     (was: 464127)
    Time Spent: 4.5h  (was: 4h 20m)

> IOUtils.contentEquals is of low performance. I will refine it.
> --------------------------------------------------------------
>
>                 Key: IO-670
>                 URL: https://issues.apache.org/jira/browse/IO-670
>             Project: Commons IO
>          Issue Type: Improvement
>            Reporter: Jin Xu
>            Priority: Critical
>         Attachments: jmh-result.org.apache.json
>
>          Time Spent: 4.5h
>  Remaining Estimate: 0h
>
> [https://github.com/apache/commons-io/pull/118]



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to