fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class MatrixTest
  9. {
  10. public int width;
  11. public int height;
  12. public int m[];
  13.  
  14. public MatrixTest(int w, int h) {
  15. this.width = w;
  16. this.height = h;
  17. this.m = new int[w * h];
  18. }
  19.  
  20. public int get(int x, int y) {
  21. return this.m[y * width + x];
  22. }
  23.  
  24. public void set(int x, int y, int value) {
  25. this.m[y * width + x] = value;
  26. }
  27.  
  28. public static void main1 (String[] args) {
  29. int w = 100, h = 200, passes = 400;
  30.  
  31. int matrix[][] = new int[h][];
  32.  
  33. for (int i = 0; i < h; ++i) {
  34. matrix[i] = new int[w];
  35. }
  36.  
  37. long start;
  38.  
  39. start = System.currentTimeMillis();
  40.  
  41. // X and Y are inverted.
  42.  
  43. for (int z = 0; z < passes; z++) {
  44. for (int x = 0; x < w; x++) {
  45. for (int y = 0; y < h; y++) {
  46. matrix[y][x]++;
  47. }
  48. }
  49. }
  50.  
  51. System.out.print("\t" + (System.currentTimeMillis() - start));
  52.  
  53. //
  54.  
  55. MatrixTest mt = new MatrixTest(w, h);
  56.  
  57. start = System.currentTimeMillis();
  58.  
  59. for (int z = 0; z < passes; z++) {
  60. for (int x = 0; x < w; x++) {
  61. for (int y = 0; y < h; y++) {
  62. mt.set(x, y, mt.get(x, y) + 1);
  63. }
  64. }
  65. }
  66.  
  67. System.out.print("\t" + (System.currentTimeMillis() - start));
  68.  
  69. //
  70.  
  71. MatrixTest mt2 = new MatrixTest(w, h);
  72.  
  73. start = System.currentTimeMillis();
  74.  
  75. for (int z = 0; z < passes; z++) {
  76. for (int x = 0; x < w; x++) {
  77. for (int y = 0; y < h; y++) {
  78. int wp = y * w + x;
  79. mt2.m[wp]++;
  80. }
  81. }
  82. }
  83.  
  84. System.out.print("\t" + (System.currentTimeMillis() - start));
  85. }
  86. public static void main (String[] args) {
  87. for(int i=0; i< 50; i++){
  88. main1(new String[]{""});
  89. System.out.println("\t");
  90. }
  91. }
  92.  
  93. }
Success #stdin #stdout 3.16s 320512KB
stdin
Standard input is empty
stdout
	30	25	34	
	25	25	34	
	23	19	20	
	22	19	21	
	20	19	20	
	23	19	20	
	22	19	20	
	22	19	20	
	23	19	20	
	23	19	20	
	23	19	21	
	23	19	20	
	24	19	20	
	24	19	20	
	24	20	20	
	23	20	20	
	20	19	20	
	20	20	20	
	20	19	20	
	20	19	20	
	19	19	20	
	20	19	21	
	19	19	20	
	19	19	21	
	19	19	21	
	20	19	20	
	20	19	20	
	20	19	20	
	20	19	20	
	20	19	20	
	20	19	20	
	20	19	20	
	19	19	20	
	20	19	20	
	20	19	20	
	19	19	20	
	20	19	20	
	20	19	20	
	20	18	21	
	20	18	21	
	19	19	21	
	19	19	21	
	20	19	20	
	20	19	20	
	20	19	20	
	20	19	20	
	19	19	21	
	19	18	21	
	20	19	20	
	20	19	20