You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@zookeeper.apache.org by asdf2014 <gi...@git.apache.org> on 2017/06/22 11:34:08 UTC

[GitHub] zookeeper pull request #290: Using `Collections.singletonList` instead of `A...

GitHub user asdf2014 opened a pull request:

    https://github.com/apache/zookeeper/pull/290

    Using `Collections.singletonList` instead of `Arrays.asList(oneElement)`

    Using `Collections.singletonList` instead of `Arrays.asList(oneElement)` for reusing a immutable object instead of creating a new object.

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/asdf2014/zookeeper coll_stuff

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/zookeeper/pull/290.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #290
    
----

----


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] zookeeper issue #290: ZOOKEEPER-2817: Using `Collections.singletonList` inst...

Posted by asdf2014 <gi...@git.apache.org>.
Github user asdf2014 commented on the issue:

    https://github.com/apache/zookeeper/pull/290
  
    Hi, @afine. Thank you for code review. It maybe due to...
    
    ```java
    // -ea -Xmx512M -Xms512M -Xmn256M -XX:+AlwaysPreTouch
    @Test
    public void testSingletonListPerformance() {
        String site = "yuzhouwan.com";
        int count = 1_0000_0000;
        long spendTime = 0;
        while (count > 0) {
            long startTime = System.nanoTime();
            Collections.singleton(site);
            long endTime = System.nanoTime();
            count--;
            spendTime += endTime - startTime;
        }
        Runtime runtime = Runtime.getRuntime();
        long totalMemory = runtime.totalMemory() / 1024 / 1024;
        long freeMemory = runtime.freeMemory() / 1024 / 1024;
        // [Collections.singleton] Spend Time: 1370786852ns = 1370.786852ms, Mem: 39/441/480 MB (diff/free/total)
        _log.info("[Collections.singleton] Spend Time: {}ns = {}ms, Mem: {}/{}/{} MB (diff/free/total)",
                spendTime, spendTime / Math.pow(10, 6), totalMemory - freeMemory, freeMemory, totalMemory);
    }
    
    // -ea -Xmx512M -Xms512M -Xmn256M -XX:+AlwaysPreTouch
    @Test
    public void testArraysAsListPerformance() {
        String site = "yuzhouwan.com";
        int count = 1_0000_0000;
        long spendTime = 0;
        while (count > 0) {
            long startTime = System.nanoTime();
            Arrays.asList(site);
            long endTime = System.nanoTime();
            count--;
            spendTime += endTime - startTime;
        }
        Runtime runtime = Runtime.getRuntime();
        long totalMemory = runtime.totalMemory() / 1024 / 1024;
        long freeMemory = runtime.freeMemory() / 1024 / 1024;
        // [Arrays.asList] Spend Time: 1549508768ns = 1549.508768ms, Mem: 195/312/507 MB (diff/free/total)
        _log.info("[Arrays.asList] Spend Time: {}ns = {}ms, Mem: {}/{}/{} MB (diff/free/total)",
                spendTime, spendTime / Math.pow(10, 6), totalMemory - freeMemory, freeMemory, totalMemory);
    }
    ```
    
    Full code is [here](https://github.com/asdf2014/yuzhouwan/blob/master/yuzhouwan-hacker/src/test/java/com/yuzhouwan/hacker/algorithms/collection/CollectionStuffTest.java#L168).


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---