Guest User

Untitled

a guest
Jul 16th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Main {
  4.  
  5. static class Point implements Comparable<Point> {
  6. long x;
  7. long y;
  8.  
  9. public Point(long x, long y) {
  10. this.x = x;
  11. this.y = y;
  12. }
  13.  
  14. @Override
  15. public int compareTo(Point o) {
  16. if (x != o.x)
  17. return Long.compare(x, o.x);
  18. return Long.compare(y, o.y);
  19. }
  20.  
  21. public boolean equals(Object o) {
  22. if (o instanceof Point)
  23. return this.compareTo((Point) o) == 0;
  24. return false;
  25. }
  26.  
  27. @Override
  28. public int hashCode() {
  29. return (int) (x ^ y);
  30. }
  31. }
  32.  
  33. static long iterator(Set<Point> A, Set<Point> B) {
  34. long startTime = System.currentTimeMillis();
  35.  
  36. long sum = 0;
  37.  
  38. for (int it = 0; it < 10; it++) {
  39. for (Point p : A) {
  40. for (Point q : B)
  41. sum += p.x * q.y;
  42. }
  43. }
  44.  
  45. long estimatedTime = System.currentTimeMillis() - startTime;
  46. System.out.printf("%d %X\n", estimatedTime, sum);
  47. return estimatedTime;
  48. }
  49.  
  50. public static void main(String[] args) {
  51. HashSet<Point> A, B;
  52. TreeSet<Point> C, D;
  53. Random rand = new Random();
  54. A = new HashSet<>();
  55. B = new HashSet<>();
  56. C = new TreeSet<>();
  57. D = new TreeSet<>();
  58.  
  59. for (int i = 0; i < 500000; i++) {
  60. long x = rand.nextLong();
  61. long y = rand.nextLong();
  62. Point p = new Point(x, y);
  63. A.add(p);
  64. C.add(p);
  65. }
  66.  
  67. for (int i = 0; i < 1; i++) {
  68. long x = rand.nextLong();
  69. long y = rand.nextLong();
  70. Point p = new Point(x, y);
  71. B.add(p);
  72. D.add(p);
  73. }
  74.  
  75. for (int i = 0; i < 10; i++) {
  76. System.out.println("HashSet");
  77. iterator(A, B);
  78. System.out.println("TreeSet");
  79. iterator(C, D);
  80. }
  81. }
  82. }
Add Comment
Please, Sign In to add comment