Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Java string frequencies benchmark

View Frequencies.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
import java.io.*;
import java.util.*;
 
public class Frequencies{
public static void main(String[] args) throws IOException{
 
 
// Open file for reading
List<String> data = new ArrayList<String>();
for(int i = 0; i < 1000000; i++)
{
data.add("Alice");
data.add("Bob");
data.add("Charlie");
data.add("Dan");
data.add("Edith");
data.add("Frank");
}
 
Map<String, Integer> result = new HashMap<String, Integer>();
 
// Start timer
final long startTime = System.nanoTime();
 
for(String item: data){
if (!result.containsKey(item))
result.put(item, 1);
else
result.put(item, result.get(item) + 1);
}
// End timer
final long endTime = System.nanoTime();
 
System.out.printf("Time elapsed: %f ms\n",
(float)(endTime - startTime) / 1e6);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.