Thread: Clearing up some Java speed myths

Page 1 of 2 12 LastLast
Results 1 to 10 of 12
  1. #1 Clearing up some Java speed myths 
    Banned

    Join Date
    Jul 2008
    Age
    26
    Posts
    5,826
    Thanks given
    1,301
    Thanks received
    197
    Rep Power
    0
    Heres a few tests to see which of the two things is faster.

    Myth: For-each loops are faster then regular For loops
    Code:
    	public static void runForLoopBenchMarks() {
    
    		double time;
    		long start;
    		int tests = 10;
    
    		int[] testArray = new int[tests];
    
    		start = System.nanoTime();
    
    		for(int a = 0; a < testArray.length; a++)
    		for(int b = 0; b < testArray.length; b++)
    		for(int c = 0; c < testArray.length; c++)
    		for(int d = 0; d < testArray.length; d++)
    		for(int e = 0; e < testArray.length; e++);
    
    		time = System.nanoTime() - start;
    		System.out.println("Time for test 1: " + time);
    
    
    		start = System.nanoTime();
    
    		for(int a : testArray)
    		for(int b : testArray)
    		for(int c : testArray)
    		for(int d : testArray)
    		for(int e : testArray);
    
    		time = System.nanoTime() - start;
    		System.out.println("Time for test 2: " + time);
    	}
    Code:
    Time for test 1: 1507376.0
    Time for test 2: 158769.0
    Result: Standard for loops are faster then for-each loops.


    Myth: Using a variable is the same speed as using a number
    Code:
    	public static void runForLoopBenchMarks() {
    
    		double time;
    		long start;
    		int tests = 10;
    
    		int[] testArray = new int[tests];
    
    		start = System.nanoTime();
    
    		for(int a = 0; a < 10; a++)
    		for(int b = 0; b < 10; b++)
    		for(int c = 0; c < 10; c++)
    		for(int d = 0; d < 10; d++)
    		for(int e = 0; e < 10; e++);
    
    		time = System.nanoTime() - start;
    		System.out.println("Time for test 1: " + time);
    
    
    		start = System.nanoTime();
    
    		for(int a = 0; a < testArray.length; a++)
    		for(int b = 0; b < testArray.length; b++)
    		for(int c = 0; c < testArray.length; c++)
    		for(int d = 0; d < testArray.length; d++)
    		for(int e = 0; e < testArray.length; e++);
    
    		time = System.nanoTime() - start;
    		System.out.println("Time for test 2: " + time);
    	}
    Code:
    Time for test 1: 1207958.0
    Time for test 2: 168002.0
    Result: Using an actual number in place of a variable will make the code faster


    Myth: Switch statements are the fastest way to check values (Below are sevaral ways compared, but note the array[] index way would be faster in the case of there being more possible number values)

    Code:
    	public static void runIntegerBenchMarks() throws Exception {
    		Test t = new Test();
    		double time;
    		long start, tests = 1000;
    		int arg = 3;
    
    
    		start = System.nanoTime();
    		for(int i = 0; i < tests; i++) {
    
    			if(arg == 1) {
    				t.choice1();
    
    			} else if(arg == 2) {
    				t.choice2();
    
    			} else if(arg == 3) {
    				t.choice3();
    			}
    		}
    		time = System.nanoTime() - start;
    		System.out.println("Time for test 1: " + time);
    		System.out.println("time for one   : " + (double)(time / tests) + "\n\n");
    
    
    		start = System.nanoTime();
    		for(int i = 0; i < tests; i++) {
    
    			switch(arg) {
    				case 1: t.choice1(); break;
    				case 2: t.choice2(); break;
    				case 3: t.choice3(); break;
    			}
    		}
    		time = System.nanoTime() - start;
    		System.out.println("Time for test 2: " + time);
    		System.out.println("time for one   : " + (double)(time / tests) + "\n\n");
    
    
    		start = System.nanoTime();
    		for(int i = 0; i < tests; i++) {
    			t.getClass().getMethod("choice" + arg).invoke(t);
    		}
    		time = System.nanoTime() - start;
    		System.out.println("Time for test 3: " + time);
    		System.out.println("time for one   : " + (double)(time / tests) + "\n\n");
    
    		Option[] options = new Option[4];
    
    		options[1] = new One();
    		options[2] = new Two();
    		options[3] = new Three();
    
    		start = System.nanoTime();
    		for(int i = 0; i < tests; i++) {
    			options[arg].choice();
    		}
    
    		time = System.nanoTime() - start;
    		System.out.println("Time for test 4: " + time);
    		System.out.println("time for one   : " + (double)(time / tests) + "\n\n");
    
    	}
    Code:
    Time for test 1: 48826.0
    time for one   : 48.826
    
    Time for test 2: 35589.0
    time for one   : 35.589
    
    Time for test 3: 2.0072777E7
    time for one   : 20072.777
    
    Time for test 4: 40004.0
    time for one   : 40.004
    Result: Switch statements are the fastest way for situations with very small possible choices, but otherwise array[] indexes would be the fastest hands down, because the time takes never increases with possible awnsers.


    Myth: List.get(int) method is slower then using an array
    Code:
    	public static void testListGet() {
    
    		double time;
    		long start;
    		int tests = 10;
    
    		List<Object> list = new ArrayList<Object>();
    		Object[] arry = new Object[100];
    
    		for(int i = 0; i < arry.length; i++) {
    			list.add(new Object());
    			arry[i] = new Object();
    		}
    
    		start = System.nanoTime();
    
    		for(int i = 0; i < tests; i++) {
    			Object o = list.get(list.size()-2);
    		}
    
    		time = System.nanoTime() - start;
    		System.out.println("Time for test 1: " + time);
    
    		start = System.nanoTime();
    
    		for(int i = 0; i < tests; i++) {
    			Object o = arry[arry.length-2];
    		}
    
    		time = System.nanoTime() - start;
    		System.out.println("Time for test 2: " + time);
    	}
    Code:
    Time for test 1: 7839.0
    Time for test 2: 1744.0
    Result: Using an array is a LOT faster than using a list






    Some misc stuff:
    Code:
    interface Option {
    
    	public void choice();
    }
    
    class One implements Option {
    
    	@Override
    	public void choice() {}
    }
    
    class Two implements Option {
    
    	@Override
    	public void choice() {}
    }
    
    class Three implements Option {
    
    	@Override
    	public void choice() {}
    }
    
    class Test {
    
    	public void choice1() {}
    	public void choice2() {}
    	public void choice3() {}
    }




    I will add more to this later.
    Reply With Quote  
     

  2. #2  
    Member Market Banned Market Banned

    Zee Best's Avatar
    Join Date
    Feb 2007
    Age
    29
    Posts
    3,036
    Thanks given
    24
    Thanks received
    210
    Rep Power
    1171
    This dosn't prove speed, considering that the application is already running the first test before the second that means there could be left over memory and other leaks.


    [Only registered and activated users can see links. ]
    Reply With Quote  
     

  3. #3  
    Banned

    Join Date
    Jul 2008
    Age
    26
    Posts
    5,826
    Thanks given
    1,301
    Thanks received
    197
    Rep Power
    0
    Quote Originally Posted by Zee best View Post
    This dosn't prove speed, considering that the application is already running the first test before the second that means there could be left over memory and other leaks.
    Memory usage doesn't affect speed.. If you don't believe me, switch it all backwards.
    Reply With Quote  
     

  4. #4  
    Banned

    Join Date
    Feb 2009
    Posts
    1,533
    Thanks given
    4
    Thanks received
    34
    Rep Power
    0
    What the fuck is an optimized loop??

    Seriously, it's called a for each loop, and has nothing to do with optimization.
    Reply With Quote  
     

  5. #5  
    Renown Programmer
    Join Date
    Dec 2008
    Posts
    363
    Thanks given
    1
    Thanks received
    28
    Rep Power
    74
    Quote Originally Posted by Java` View Post
    Memory usage doesn't affect speed.. If you don't believe me, switch it all backwards.
    If you're swapping, it most definitely will result in major speed loss.
    [Only registered and activated users can see links. ]
    Reply With Quote  
     

  6. #6  
    Banned

    Join Date
    Jul 2008
    Age
    26
    Posts
    5,826
    Thanks given
    1,301
    Thanks received
    197
    Rep Power
    0
    Quote Originally Posted by popbob View Post
    What the fuck is an optimized loop??

    Seriously, it's called a for each loop, and has nothing to do with optimization.
    Nice, comment, what does it have to do with this thread?

    Quote Originally Posted by Parabolika View Post
    If you're swapping, it most definitely will result in major speed loss.
    I have 8GB RAM.
    Also, my page file is disabled.

    So yeah.
    Reply With Quote  
     

  7. #7  
    Banned

    Join Date
    Jan 2009
    Age
    28
    Posts
    2,662
    Thanks given
    66
    Thanks received
    207
    Rep Power
    0
    i have a question which one of these is faster?


    Code:
    switch(pE[playerWeapon]){
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
    case 7:
    case 8:
    case 9:
    case 10:
    case 11:
    case 12:
    case 13:
    case 14:
    case 15:
    case 16:
    case 17:
    case 18:
    case 19:
    case 20:
    c.showingSpecial(0);
    break;
    or would this be better?

    Code:
    int[] SpecWeps = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
    
    for( SP : SpecWeps){
    if(pE[playerWeapon] == SP){
    c.ShowingSpec(0);
    } else {
    c.ShowingSpec(1);
    }
    }
    Reply With Quote  
     

  8. #8  
    Officially Retired

    Huey's Avatar
    Join Date
    Jan 2008
    Age
    19
    Posts
    16,489
    Thanks given
    3,387
    Thanks received
    7,726
    Rep Power
    5000
    Quote Originally Posted by Java` View Post
    Heres a few tests to see which of the two things is faster.

    Myth: For-each loops are faster then regular For loops
    Code:
    	public static void runForLoopBenchMarks() {
    
    		double time;
    		long start;
    		int tests = 10;
    
    		int[] testArray = new int[tests];
    
    		start = System.nanoTime();
    
    		for(int a = 0; a < testArray.length; a++)
    		for(int b = 0; b < testArray.length; b++)
    		for(int c = 0; c < testArray.length; c++)
    		for(int d = 0; d < testArray.length; d++)
    		for(int e = 0; e < testArray.length; e++);
    
    		time = System.nanoTime() - start;
    		System.out.println("Time for test 1: " + time);
    
    
    		start = System.nanoTime();
    
    		for(int a : testArray)
    		for(int b : testArray)
    		for(int c : testArray)
    		for(int d : testArray)
    		for(int e : testArray);
    
    		time = System.nanoTime() - start;
    		System.out.println("Time for test 2: " + time);
    	}
    Code:
    Time for test 1: 1507376.0
    Time for test 2: 158769.0
    Result: Standard for loops are faster then for-each loops.


    Myth: Using a variable is the same speed as using a number
    Code:
    	public static void runForLoopBenchMarks() {
    
    		double time;
    		long start;
    		int tests = 10;
    
    		int[] testArray = new int[tests];
    
    		start = System.nanoTime();
    
    		for(int a = 0; a < 10; a++)
    		for(int b = 0; b < 10; b++)
    		for(int c = 0; c < 10; c++)
    		for(int d = 0; d < 10; d++)
    		for(int e = 0; e < 10; e++);
    
    		time = System.nanoTime() - start;
    		System.out.println("Time for test 1: " + time);
    
    
    		start = System.nanoTime();
    
    		for(int a = 0; a < testArray.length; a++)
    		for(int b = 0; b < testArray.length; b++)
    		for(int c = 0; c < testArray.length; c++)
    		for(int d = 0; d < testArray.length; d++)
    		for(int e = 0; e < testArray.length; e++);
    
    		time = System.nanoTime() - start;
    		System.out.println("Time for test 2: " + time);
    	}
    Code:
    Time for test 1: 1207958.0
    Time for test 2: 168002.0
    Result: Using an actual number in place of a variable will make the code faster


    Myth: Switch statements are the fastest way to check values (Below are sevaral ways compared, but note the array[] index way would be faster in the case of there being more possible number values)

    Code:
    	public static void runIntegerBenchMarks() throws Exception {
    		Test t = new Test();
    		double time;
    		long start, tests = 1000;
    		int arg = 3;
    
    
    		start = System.nanoTime();
    		for(int i = 0; i < tests; i++) {
    
    			if(arg == 1) {
    				t.choice1();
    
    			} else if(arg == 2) {
    				t.choice2();
    
    			} else if(arg == 3) {
    				t.choice3();
    			}
    		}
    		time = System.nanoTime() - start;
    		System.out.println("Time for test 1: " + time);
    		System.out.println("time for one   : " + (double)(time / tests) + "\n\n");
    
    
    		start = System.nanoTime();
    		for(int i = 0; i < tests; i++) {
    
    			switch(arg) {
    				case 1: t.choice1(); break;
    				case 2: t.choice2(); break;
    				case 3: t.choice3(); break;
    			}
    		}
    		time = System.nanoTime() - start;
    		System.out.println("Time for test 2: " + time);
    		System.out.println("time for one   : " + (double)(time / tests) + "\n\n");
    
    
    		start = System.nanoTime();
    		for(int i = 0; i < tests; i++) {
    			t.getClass().getMethod("choice" + arg).invoke(t);
    		}
    		time = System.nanoTime() - start;
    		System.out.println("Time for test 3: " + time);
    		System.out.println("time for one   : " + (double)(time / tests) + "\n\n");
    
    		Option[] options = new Option[4];
    
    		options[1] = new One();
    		options[2] = new Two();
    		options[3] = new Three();
    
    		start = System.nanoTime();
    		for(int i = 0; i < tests; i++) {
    			options[arg].choice();
    		}
    
    		time = System.nanoTime() - start;
    		System.out.println("Time for test 4: " + time);
    		System.out.println("time for one   : " + (double)(time / tests) + "\n\n");
    
    	}
    Code:
    Time for test 1: 48826.0
    time for one   : 48.826
    
    Time for test 2: 35589.0
    time for one   : 35.589
    
    Time for test 3: 2.0072777E7
    time for one   : 20072.777
    
    Time for test 4: 40004.0
    time for one   : 40.004
    Result: Switch statements are the fastest way for situations with very small possible choices, but otherwise array[] indexes would be the fastest hands down, because the time takes never increases with possible awnsers.


    Myth: List.get(int) method is slower then using an array
    Code:
    	public static void testListGet() {
    
    		double time;
    		long start;
    		int tests = 10;
    
    		List<Object> list = new ArrayList<Object>();
    		Object[] arry = new Object[100];
    
    		for(int i = 0; i < arry.length; i++) {
    			list.add(new Object());
    			arry[i] = new Object();
    		}
    
    		start = System.nanoTime();
    
    		for(int i = 0; i < tests; i++) {
    			Object o = list.get(list.size()-2);
    		}
    
    		time = System.nanoTime() - start;
    		System.out.println("Time for test 1: " + time);
    
    		start = System.nanoTime();
    
    		for(int i = 0; i < tests; i++) {
    			Object o = arry[arry.length-2];
    		}
    
    		time = System.nanoTime() - start;
    		System.out.println("Time for test 2: " + time);
    	}
    Code:
    Time for test 1: 7839.0
    Time for test 2: 1744.0
    Result: Using an array is a LOT faster than using a list






    Some misc stuff:
    Code:
    interface Option {
    
    	public void choice();
    }
    
    class One implements Option {
    
    	@Override
    	public void choice() {}
    }
    
    class Two implements Option {
    
    	@Override
    	public void choice() {}
    }
    
    class Three implements Option {
    
    	@Override
    	public void choice() {}
    }
    
    class Test {
    
    	public void choice1() {}
    	public void choice2() {}
    	public void choice3() {}
    }




    I will add more to this later.


    looks like someone goes to java forums alot and copy and paste

    Listen children don't become this guy.
    Quote Originally Posted by Owner Spikey View Post
    Why can I attack lower level npc's in a matter of a mouse hover but for a higher level npc the only choice to attack is by right clicking option attack?

    Reply With Quote  
     

  9. #9  
    uber haxur
    Guest
    Quote Originally Posted by Java` View Post
    Nice, comment, what does it have to do with this thread?
    You edited out before someone else pointed out your idiocy, looool.
    Reply With Quote  
     

  10. #10  
    Banned

    Join Date
    Jul 2008
    Age
    26
    Posts
    5,826
    Thanks given
    1,301
    Thanks received
    197
    Rep Power
    0
    Quote Originally Posted by digistr View Post
    i have a question which one of these is faster?


    Code:
    switch(pE[playerWeapon]){
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
    case 7:
    case 8:
    case 9:
    case 10:
    case 11:
    case 12:
    case 13:
    case 14:
    case 15:
    case 16:
    case 17:
    case 18:
    case 19:
    case 20:
    c.showingSpecial(0);
    break;
    or would this be better?

    Code:
    int[] SpecWeps = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
    
    for( SP : SpecWeps){
    if(pE[playerWeapon] == SP){
    c.ShowingSpec(0);
    } else {
    c.ShowingSpec(1);
    }
    }
    Switch statement is faster, the second once is smaller though.

    Quote Originally Posted by T.i. View Post
    looks like someone goes to java forums alot and copy and paste
    Thats me.. Look who posted the code there and then,
    [Only registered and activated users can see links. ]

    Quote Originally Posted by uber haxur View Post
    You edited out before someone else pointed out your idiocy, looool.
    Thats the joke.

    Congratulations, you just won a big fuck in the ass.
    Reply With Quote  
     

Page 1 of 2 12 LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •