Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Java string frequencies benchmark
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.