[
https://issues.apache.org/jira/browse/ZOOKEEPER-2817?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16069334#comment-16069334
]
ASF GitHub Bot commented on ZOOKEEPER-2817:
-------------------------------------------
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).
> Using `Collections.singletonList` instead of `Arrays.asList(oneElement)`
> ------------------------------------------------------------------------
>
> Key: ZOOKEEPER-2817
> URL: https://issues.apache.org/jira/browse/ZOOKEEPER-2817
> Project: ZooKeeper
> Issue Type: Improvement
> Components: server
> Affects Versions: 3.5.3
> Reporter: Benedict Jin
> Assignee: Benedict Jin
> Fix For: 3.5.4, 3.6.0
>
> Original Estimate: 72h
> Remaining Estimate: 72h
>
> Using `Collections.singletonList` instead of `Arrays.asList(oneElement)` for
> reusing a immutable object instead of creating a new object.
--
This message was sent by Atlassian JIRA
(v6.4.14#64029)