Skip to content

Instantly share code, notes, and snippets.

@struberg
Created May 21, 2021 18:52
Show Gist options
  • Save struberg/ad08ea718fbd68b69e0ce4a20981fcf0 to your computer and use it in GitHub Desktop.
Save struberg/ad08ea718fbd68b69e0ce4a20981fcf0 to your computer and use it in GitHub Desktop.
bench a few ways to sort a Map. Especially compare speed of TreeMap vs sorted Streams
/*
* 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 at.rise.docarchive;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
/**
* turned out TreeMap is still 20% faster than sorted Stream.
*
* @author <a href="mailto:struberg@apache.org">Mark Struberg</a>
*/
public class MapSortTest {
private final static int ITERATIONS = 5_000_000;
private static Map<String, String> vals = new HashMap<>();
static {
vals.put("a", "b");
vals.put("2a", "sdfb");
vals.put("d", "sdfb");
vals.put("3wjehrwa", "sdfb");
vals.put("2a", "sdfb");
vals.put("wglkjöa", "sdfb");
vals.put("elkasdfa", "sdfb");
vals.put("fasdfa", "sdfb");
vals.put("xfafasa", "sdfb");
vals.put("faasdf", "sdfb");
vals.put("3fasdfa", "sdfb");
vals.put("2xfafasa", "sdfb");
vals.put("4faasdf", "sdfb");
};
@Test
public void testSameResult() {
String vT = sortTreeMap(vals);
String vS = sortStream(vals);
assertEquals(vT.length(), vS.length());
}
@Test
public void testMap() {
long totalsize = 0L;
// warmup
for (int i=0; i<1000; i++) {
totalsize += sortTreeMap(vals).length();
}
totalsize = 0L;
long start = System.nanoTime();
for (int i=0; i<ITERATIONS; i++) {
totalsize += sortTreeMap(vals).length();
//X totalsize += sortStream(vals).length();
}
long end = System.nanoTime();
System.out.println(totalsize + "took " + TimeUnit.NANOSECONDS.toMillis(end - start));
}
private String sortTreeMap(Map<String, String> val) {
TreeMap<String, String> sorted = new TreeMap<>(val);
StringBuilder sb = new StringBuilder(2048);
for (Map.Entry<String, String> e : sorted.entrySet()) {
sb.append(e.getKey()).append('=').append(e.getValue()).append(';');
}
return sb.toString();
}
private String sortStream(Map<String, String> val) {
StringBuilder sb = new StringBuilder(2048);
val.entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.forEach(e -> sb.append(e.getKey()).append('=').append(e.getValue()).append(';'));
return sb.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment