Skip to content

Instantly share code, notes, and snippets.

@raghavam
Created July 6, 2014 15:09
Show Gist options
  • Save raghavam/2be48a98cae31c418678 to your computer and use it in GitHub Desktop.
Save raghavam/2be48a98cae31c418678 to your computer and use it in GitHub Desktop.
//max has been set to 1000000;
private void writeTest(Map<Integer, Integer> map, int max) {
long startTime = System.nanoTime();
for(int i = 1; i <= max; i++) {
Integer a = new Integer(i);
map.put(a, a);
}
long endTime = System.nanoTime();
double diff = (endTime - startTime)/(double)1000000000;
System.out.println("Time taken to write (RAM): " + diff);
Jedis jedis = new Jedis("localhost", 6379);
jedis.flushAll();
Pipeline p = jedis.pipelined();
startTime = System.nanoTime();
for(int i = 1; i <= max; i++) {
String a = Integer.toString(i);
p.set(a, a);
}
p.sync();
endTime = System.nanoTime();
diff = (endTime - startTime)/(double)1000000000;
System.out.println("Time taken to write (Redis): " + diff);
jedis.close();
PrintWriter writer = null;
try {
writer = new PrintWriter(new BufferedWriter(
new FileWriter("test.out")));
startTime = System.nanoTime();
for(int i = 1; i <= max; i++)
writer.println(i);
writer.flush();
endTime = System.nanoTime();
diff = (endTime - startTime)/(double)1000000000;
System.out.println("Time taken to write (File): " + diff);
}catch(Exception e) {
e.printStackTrace();
}finally {
if(writer != null)
writer.close();
}
}
private void readTest(Map<Integer, Integer> map, int max) {
Set<Integer> keys = map.keySet();
long startTime = System.nanoTime();
for(Integer key : keys)
map.get(key);
long endTime = System.nanoTime();
double diff = (endTime - startTime)/(double)1000000000;
System.out.println("Time taken to read (RAM): " + diff);
Jedis jedis = new Jedis("localhost", 6379);
Pipeline p = jedis.pipelined();
startTime = System.nanoTime();
for(int i = 1; i <= max; i++) {
String a = Integer.toString(i);
p.get(a);
}
p.sync();
endTime = System.nanoTime();
diff = (endTime - startTime)/(double)1000000000;
System.out.println("Time taken to read (Redis): " + diff);
jedis.flushAll();
jedis.close();
Scanner scanner = null;
try {
scanner = new Scanner(new BufferedReader(
new FileReader("test.out")));
startTime = System.nanoTime();
while(scanner.hasNext())
scanner.nextLine();
endTime = System.nanoTime();
diff = (endTime - startTime)/(double)1000000000;
System.out.println("Time taken to read (File): " + diff);
}catch(Exception e) {
e.printStackTrace();
}finally {
if(scanner != null)
scanner.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment