Advertisement
Guest User

Efficiency Deficiency

a guest
Oct 23rd, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. CODU MEU:
  2.  
  3. public class DoubleDataFrame implements DataFrame<Double>
  4. {
  5. private double[][] data;
  6. private List<String> columnNames;
  7. private Map<String,Integer> nameIndices = new HashMap<>();
  8.  
  9. public DoubleDataFrame(List<String> columnNames, double [][] data) {
  10. this.data = data;
  11. this.columnNames = new ArrayList<>(columnNames);
  12. int i=0;
  13. for(String name : columnNames) {
  14. nameIndices.put(name, i);
  15. i++;
  16. }
  17. }
  18.  
  19. @Override
  20. public int getRowCount() {
  21.  
  22. return this.data.length;
  23. }
  24.  
  25. @Override
  26. public int getColumnCount() {
  27.  
  28. return this.getColumnNames().size();
  29. }
  30.  
  31. @Override
  32. public List<String> getColumnNames() {
  33. List<String> immutableColumnNames = Collections.unmodifiableList(this.columnNames);
  34. return immutableColumnNames;
  35. }
  36.  
  37. @Override
  38. public void setValue(int rowIndex, String colName, Double value)
  39. throws IndexOutOfBoundsException, IllegalArgumentException {
  40. if (rowIndex<0 || rowIndex>this.getRowCount()) {
  41. throw new IndexOutOfBoundsException("Invalid row index provided.");
  42. }
  43. if (!this.getColumnNames().contains(colName)) {
  44. throw new IllegalArgumentException("Non-existing column name.");
  45. }
  46. this.data[rowIndex][nameIndices.get(colName)] = value;
  47.  
  48. }
  49.  
  50. @Override
  51. public Double getValue(int rowIndex, String colName) throws IndexOutOfBoundsException, IllegalArgumentException {
  52. if (rowIndex<0 || rowIndex>this.getRowCount()) {
  53. throw new IndexOutOfBoundsException("Invalid row index provided.");
  54. }
  55. if (!this.getColumnNames().contains(colName)) {
  56. throw new IllegalArgumentException("Non-existing column name.");
  57. }
  58. return data[rowIndex][nameIndices.get(colName)];
  59. }
  60.  
  61. TESTERU:
  62.  
  63. int size = 10000;
  64. double [][] data = new double [1][ size ];
  65. List < String > header = new ArrayList < >( size ) ;
  66. for (int j =0; j < size ; j ++)
  67. {
  68. data [0][ j ] = j ;
  69. header . add ("x_"+ j ) ;
  70. }
  71. DoubleDataFrame df = new DoubleDataFrame ( header , data ) ;
  72. long time = System . currentTimeMillis () ;
  73. for (int j =0; j < size ; j ++)
  74. {
  75. df . getValue (0 , header . get ( j ) ) ;
  76. df . setValue (0 , header . get ( j ) , 0d ) ;
  77. }
  78. time = System . currentTimeMillis () - time ;
  79. System . out . println (" Running time : "+ time +"ms") ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement