Skip to content

Instantly share code, notes, and snippets.

@danboykis
Last active January 3, 2016 05:59
Show Gist options
  • Save danboykis/8420057 to your computer and use it in GitHub Desktop.
Save danboykis/8420057 to your computer and use it in GitHub Desktop.
import com.github.krukow.clj_ds.PersistentMap;
import com.github.krukow.clj_ds.Persistents;
import org.junit.Test;
import org.junit.Ignore;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
public class MapTests {
Random random = new Random();
private Long randomLong() {
Long x = random.nextLong();
while(x < 0) x = random.nextLong();
return x;
}
@Test
public void test() {
long start = System.nanoTime();
PersistentMap<Long,Integer> pmap = Persistents.hashMap();
for(int i=0; i<10000; i++) {
pmap = pmap.plus(randomLong(),i);
}
long end = System.nanoTime();
System.out.println(end-start);
}
@Test
@Ignore
public void testMut() {
long start = System.nanoTime();
Map<Long,Integer> map = new HashMap<Long, Integer>();
for(int i=0; i<10000; i++) {
map.put(randomLong(),i);
}
long end = System.nanoTime();
System.out.println(end-start);
}
@Test
public void testConc() {
long start = System.nanoTime();
Map<Long,Integer> map = new ConcurrentHashMap<Long, Integer>();
for(int i=0; i<10000; i++) {
map.put(randomLong(), i);
}
long end = System.nanoTime();
System.out.println(end-start);
}
@Test
public void testSync() {
long start = System.nanoTime();
Map<Long,Integer> map = Collections.synchronizedMap(new HashMap<Long, Integer>());
for(int i=0; i<10000; i++) {
map.put(randomLong(),i);
}
long end = System.nanoTime();
System.out.println(end-start);
}
}
/*
I think the github project has them.
Single threaded write up: https://gist.github.com/danboykis/8420057
Immutable: 101324000
Mutable: 19731000
Conc: 33547000
Sync: 21788000
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment