polectron

Untitled

Feb 11th, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. package alg717254455R.s2;
  2.  
  3. import java.util.Random;
  4.  
  5. public class Loop5 {
  6. public static void loop5(int n) {
  7. /*Quadratic algorithm O(n^4) */
  8. Random rn = new Random();
  9. @SuppressWarnings("unused")
  10. int cont=0;
  11. for (int i = 0; i < n * n * n; i++) {
  12. for (int j = 1; j <= n; j *= 2) {
  13. cont += rn.nextInt();
  14. }
  15. }
  16. }
  17.  
  18. public static void main(String arg[]) {
  19. long t1,t2;
  20. int nTimes = Integer.parseInt(arg[0]);
  21.  
  22. for (int n=1; n<=100000; n*=2) {
  23. t1 = System.currentTimeMillis();
  24.  
  25. for (int repetitions=1; repetitions<=nTimes; repetitions++){
  26. loop5(n);
  27. }
  28.  
  29. t2 = System.currentTimeMillis();
  30. System.out.println ("n="+n+ "**TIME=" +(t2-t1)+ " ** nTimes=" + nTimes);
  31. } //for
  32. } //main
  33. }
Add Comment
Please, Sign In to add comment