Skip to content

Instantly share code, notes, and snippets.

@shtratos
Created February 12, 2016 16:28
Show Gist options
  • Save shtratos/4ac95bc0de3ce411c782 to your computer and use it in GitHub Desktop.
Save shtratos/4ac95bc0de3ce411c782 to your computer and use it in GitHub Desktop.
hashmap copy benchmark
import org.openjdk.jmh.annotations.Benchmark;
import java.util.HashMap;
import java.util.Map;
public class Benchmarks {
private static final Map<String, String> STRINGS;
static {
STRINGS = new HashMap<>();
STRINGS.put("test1", "value1");
STRINGS.put("test2", "value2");
STRINGS.put("test3", "value3");
STRINGS.put("test4", "value4");
STRINGS.put("test5", "value5");
}
private static final HashMap<String, String> PROPERTIES = new HashMap<>(STRINGS);
static class Entity {
private HashMap<String, String> properties = new HashMap<>();
public HashMap<String, String> getProperties() {
return properties;
}
public void setProperties(HashMap<String, String> properties) {
this.properties = properties;
}
public void copyProperties(Map<String, String> properties) {
this.properties = new HashMap<>(properties);
}
}
private static final Entity entity = new Entity();
@Benchmark
public void measureAssignment() {
entity.setProperties(PROPERTIES);
}
@Benchmark
public void measureCopy() {
entity.copyProperties(PROPERTIES);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment