Created
February 12, 2016 16:28
-
-
Save shtratos/4ac95bc0de3ce411c782 to your computer and use it in GitHub Desktop.
hashmap copy benchmark
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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