ZOOKEEPER-2817: Using Collections.singletonList
instead of Arrays.asList(oneElement)
#290
+19
−15
Conversation
Collections.singletonList
instead of Arrays.asList(oneElement)
Collections.singletonList
instead of Arrays.asList(oneElement)
This seems like a very small optimization for a part of the code that is not performance sensitive. The change creates inconsistencies in |
Hi, @afine. Thank you for code review. It maybe due to... // -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. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Using
Collections.singletonList
instead ofArrays.asList(oneElement)
for reusing a immutable object instead of creating a new object.