fork(1) download
  1. import java.util.Random;
  2.  
  3. final class CashAccountRow
  4. {
  5. public int code; // 0 - 1000000
  6. public boolean gender; // 0 - 1
  7. public byte age; // 0 - 100
  8. public int amount_of_money; // 0 - 1000000
  9. public short height; // 0 – 300
  10.  
  11. public static CashAccountRow[] generateTable (int n)
  12. {
  13. CashAccountRow[] cashAccountTable = new CashAccountRow[n];
  14. Random randomGenerator = new Random();
  15. for (int i = 0; i < cashAccountTable.length; i++)
  16. {
  17. cashAccountTable[i] = new CashAccountRow();
  18. cashAccountTable[i].code = randomGenerator.nextInt(1000000);
  19. cashAccountTable[i].gender = randomGenerator.nextBoolean();
  20. cashAccountTable[i].age = (byte)randomGenerator.nextInt(100);
  21. cashAccountTable[i].amount_of_money = randomGenerator.nextInt(1000000);
  22. cashAccountTable[i].height = (short)randomGenerator.nextInt(300);
  23. }
  24. return cashAccountTable;
  25. }
  26.  
  27. public final boolean rowInCondition(RangeFilter filter)
  28. {
  29. return (
  30. (!filter.useFilter[0] || this.code <= filter.end.code && this.code >= filter.begin.code ) &&
  31. (!filter.useFilter[1] || this.gender == filter.end.gender && this.gender == filter.begin.gender ) &&
  32. (!filter.useFilter[2] || this.age <= filter.end.age && this.age >= filter.begin.age ) &&
  33. (!filter.useFilter[3] || this.amount_of_money <= filter.end.amount_of_money && this.amount_of_money >= filter.begin.amount_of_money ) &&
  34. (!filter.useFilter[4] || this.height <= filter.end.height && this.height >= filter.begin.height)
  35. );
  36.  
  37. }
  38.  
  39. public static int rowsInCondition(RangeFilter filter, CashAccountRow[] table)
  40. {
  41. int result = 0;
  42. for (int i = 0; i < table.length; i++)
  43. if (
  44. (!filter.useFilter[0] || table[i].code <= filter.end.code && table[i].code >= filter.begin.code ) &&
  45. (!filter.useFilter[1] || table[i].gender == filter.end.gender && table[i].gender == filter.begin.gender ) &&
  46. (!filter.useFilter[2] || table[i].age <= filter.end.age && table[i].age >= filter.begin.age ) &&
  47. (!filter.useFilter[3] || table[i].amount_of_money <= filter.end.amount_of_money && table[i].amount_of_money >= filter.begin.amount_of_money ) &&
  48. (!filter.useFilter[4] || table[i].height <= filter.end.height && table[i].height >= filter.begin.height)
  49. )
  50. result++;
  51. return result;
  52. }
  53. }
  54.  
  55. class RangeFilter
  56. {
  57. final CashAccountRow begin;
  58. final CashAccountRow end;
  59. final boolean [] useFilter;
  60. public RangeFilter(CashAccountRow begin, CashAccountRow end, boolean [] useFilter)
  61. {
  62. this.begin = begin;
  63. this.end = end;
  64. this.useFilter = useFilter;
  65. }
  66. public static RangeFilter generateRangeFilter ()
  67. {
  68. Random randomGenerator = new Random();
  69. boolean [] useFilter = new boolean[5];
  70. for(int i = 0; i < useFilter.length; i++)
  71. {
  72. useFilter[i] = randomGenerator.nextBoolean();
  73. }
  74. CashAccountRow begin = new CashAccountRow();
  75. begin.code = randomGenerator.nextInt(500000);
  76. begin.gender = randomGenerator.nextBoolean();
  77. begin.age = (byte)randomGenerator.nextInt(50);
  78. begin.amount_of_money = randomGenerator.nextInt(500000);
  79. begin.height = (short)randomGenerator.nextInt(150);
  80.  
  81. CashAccountRow end = new CashAccountRow();
  82. end.code = begin.code + randomGenerator.nextInt(500000);
  83. end.gender = begin.gender;
  84. end.age = (byte)(begin.age + randomGenerator.nextInt(50));
  85. end.amount_of_money = begin.amount_of_money + randomGenerator.nextInt(500000);
  86. end.height = (short)(begin.height + randomGenerator.nextInt(150));
  87.  
  88. return new RangeFilter(begin, end, useFilter);
  89. }
  90. }
  91.  
  92.  
  93. public class Main
  94. {
  95. public static void test1(RangeFilter filter, CashAccountRow [] table)
  96. {
  97. long start = System.nanoTime();
  98. int result = 0;
  99. for (int i = 0; i < table.length; i++)
  100. if (table[i].rowInCondition(filter)) result++;
  101. long elapsedTime = System.nanoTime() - start;
  102. System.out.println("Result = " + result);
  103. System.out.println("Elapsed time = " + elapsedTime/1000000.0 + "ms");
  104. }
  105.  
  106. public static void test2(RangeFilter filter, CashAccountRow [] table)
  107. {
  108.  
  109. long start = System.nanoTime();
  110. int result = CashAccountRow.rowsInCondition(filter, table);
  111. long elapsedTime = System.nanoTime() - start;
  112. System.out.println("Result = " + result);
  113. System.out.println("Elapsed time = " + elapsedTime/1000000.0 + "ms");
  114. }
  115.  
  116. public static void main (String[] agrs)
  117. {
  118. CashAccountRow [] table = CashAccountRow.generateTable (3300000);
  119. RangeFilter filter = RangeFilter.generateRangeFilter();
  120. System.out.println("test1");
  121. test1(filter, table);
  122. System.out.println("test2");
  123. test2(filter, table);
  124. }
  125. }
  126.  
Success #stdin #stdout 1.67s 380160KB
stdin
Standard input is empty
stdout
test1
Result = 94323
Elapsed time = 77.213802ms
test2
Result = 94323
Elapsed time = 57.728647ms