Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- /**
- * @author Boris Minaev, e-mail: [email protected]
- */
- public class Testing {
- FastScanner in;
- PrintWriter out;
- // that's brute implementation of changing X & Y of an array
- int[][] simpleMethod(int[][] a) {
- int n = a.length;
- int m = a[0].length;
- int[][] b = new int[m][n];
- for (int i = 0; i < n; i++)
- for (int j = 0; j < m; j++)
- b[j][i] = a[i][j];
- return b;
- }
- // magic constant, isn't it?
- // don't change it!
- static final int MAGIC_CONST = 150;
- // same function as 'simpleMethod', but there is something strange
- // some magic here, don't look!
- int[][] magicFunction(int[][] a) {
- int n = a.length;
- int m = a[0].length;
- int w = (n - 1) / MAGIC_CONST + 1;
- int h = (m - 1) / MAGIC_CONST + 1;
- int[][][] tmp = new int[w][h][MAGIC_CONST * MAGIC_CONST];
- for (int i1 = 0; i1 < w; i1++)
- for (int j1 = 0; j1 < h; j1++) {
- int toK = Math.min(MAGIC_CONST, m - j1 * MAGIC_CONST);
- int toL = Math.min(MAGIC_CONST, n - i1 * MAGIC_CONST);
- for (int k1 = 0; k1 < toK; k1++) {
- for (int l1 = 0; l1 < toL; l1++) {
- tmp[i1][j1][k1 * MAGIC_CONST + l1] = a[i1 * MAGIC_CONST
- + l1][j1 * MAGIC_CONST + k1];
- }
- }
- }
- int[][] b = new int[m][n];
- for (int i1 = 0; i1 < w; i1++)
- for (int j1 = 0; j1 < h; j1++) {
- int toK = Math.min(MAGIC_CONST, m - j1 * MAGIC_CONST);
- int toL = Math.min(MAGIC_CONST, n - i1 * MAGIC_CONST);
- for (int k1 = 0; k1 < toK; k1++) {
- for (int l1 = 0; l1 < toL; l1++) {
- b[j1 * MAGIC_CONST + k1][i1 * MAGIC_CONST + l1] = tmp[i1][j1][k1
- * MAGIC_CONST + l1];
- }
- }
- }
- return b;
- }
- void solve() {
- // size of array
- int n = 5678;
- int m = 6574;
- int[][] a = new int[n][m];
- //fill the array with a random data
- Random rnd = new Random();
- for (int i = 0; i < n; i++)
- for (int j = 0; j < m; j++)
- a[i][j] = rnd.nextInt();
- long timeStart = System.nanoTime();
- int[][] b = simpleMethod(a);
- long totalTime = System.nanoTime() - timeStart;
- out.println("Simple implementation: " + totalTime / 1000000.0 + " ms");
- timeStart = System.nanoTime();
- int[][] b2 = magicFunction(a);
- totalTime = System.nanoTime() - timeStart;
- out.println("Magic implementation: " + totalTime / 1000000.0 + " ms");
- // check that two results are same
- for (int i = 0; i < m; i++)
- for (int j = 0; j < n; j++)
- if (b[i][j] != b2[i][j]) {
- System.err.print("FAIL is here!");
- return;
- }
- }
- void run() {
- try {
- in = new FastScanner(new File("tesing.in"));
- out = new PrintWriter(new File("tesing.out"));
- solve();
- out.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- }
- void runIO() {
- in = new FastScanner(System.in);
- out = new PrintWriter(System.out);
- solve();
- out.close();
- }
- class FastScanner {
- BufferedReader br;
- StringTokenizer st;
- public FastScanner(File f) {
- try {
- br = new BufferedReader(new FileReader(f));
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- }
- public FastScanner(InputStream f) {
- br = new BufferedReader(new InputStreamReader(f));
- }
- String next() {
- while (st == null || !st.hasMoreTokens()) {
- String s = null;
- try {
- s = br.readLine();
- } catch (IOException e) {
- e.printStackTrace();
- }
- if (s == null)
- return null;
- st = new StringTokenizer(s);
- }
- return st.nextToken();
- }
- boolean hasMoreTokens() {
- while (st == null || !st.hasMoreTokens()) {
- String s = null;
- try {
- s = br.readLine();
- } catch (IOException e) {
- e.printStackTrace();
- }
- if (s == null)
- return false;
- st = new StringTokenizer(s);
- }
- return true;
- }
- int nextInt() {
- return Integer.parseInt(next());
- }
- long nextLong() {
- return Long.parseLong(next());
- }
- }
- public static void main(String[] args) {
- new Testing().runIO();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment