Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.56 KB | None | 0 0
  1. public class DicoPerformanceTest {
  2. final static int NB_WORDS = 100000;
  3. final static int NB_LOOKUPS = 1000;
  4. /**
  5. * @param args
  6. */
  7. public static void main(String[] args) {
  8. IDictionary dico1 = new ArrayListDictionary();
  9. runExperiment("populate dico 1 (" + NB_WORDS + " words)", new
  10. DictionaryRandomPopulator(dico1,
  11. NB_WORDS));
  12. runExperiment("look up dico 1 (" + NB_LOOKUPS + " search)",
  13. new DictionaryLookupExperimenter(dico1, NB_LOOKUPS));
  14. }
  15. static void runExperiment(String name, Runnable r) {
  16. System.out.println("start " + name);
  17. long duration = System.currentTimeMillis();
  18. r.run();
  19. duration = System.currentTimeMillis() - duration;
  20. System.out.println(name + " executed in " + duration + " ms");
  21. }
  22. //
  23. // dictionary interface and implementation
  24. //
  25. static interface IDictionary {
  26. void put(String word, String definition);
  27. String getDefinition(String word);
  28. }
  29. static class ArrayListDictionary implements IDictionary {
  30. List<String> _wordsList = new ArrayList<String>();
  31. List<String> _definitionsList = new ArrayList<String>();
  32. @Override
  33. public void put(String word, String definition) {
  34. _wordsList.add(word);
  35. _definitionsList.add(definition);
  36. }
  37. @Override
  38. public String getDefinition(String word) {
  39. int index = _wordsList.indexOf(word);
  40. if (index >= 0) {
  41. _definitionsList.get(index);
  42. }
  43. // return null if word is not found in dictionary
  44. return null;
  45. }
  46. }
  47. //
  48. // classes for experiment
  49. //
  50. static class WordGenerator {
  51. public static String generate(int i) {
  52. return "fake_word_" + i;
  53. }
  54. public static String generateRandom(Random random) {
  55. return generate(random.nextInt(NB_WORDS));
  56. }
  57. }
  58. static class DictionaryRandomPopulator implements Runnable {
  59. private IDictionary _dictionary;
  60. private int _size;
  61. private Random _random = new Random(0);
  62. DictionaryRandomPopulator(IDictionary dictionary, int size) {
  63. super();
  64. _dictionary = dictionary;
  65. _size = size;
  66. }
  67. @Override
  68. public void run() {
  69. for (int i = 0; i < _size; i++) {
  70. _dictionary.put(WordGenerator.generate(i),
  71. generateDefinition(i));
  72. }
  73. }
  74. private String generateDefinition(int i) {
  75. return "fake definition for word " + i + " with random part: "
  76. + _random.nextInt();
  77. }
  78. }
  79. static class DictionaryLookupExperimenter implements Runnable {
  80. private IDictionary _dictionary;
  81. private int _number;
  82. private Random _random = new Random(0);
  83. DictionaryLookupExperimenter(IDictionary dictionary, int number) {
  84. super();
  85. _dictionary = dictionary;
  86. _number = number;
  87. }
  88. @Override
  89. public void run() {
  90. for (int i = 0; i < _number; i++) {
  91. _dictionary
  92. .getDefinition(WordGenerator.generateRandom(_random));
  93. }
  94. }
  95. }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement