Results 1 to 5 of 5
  1. #1
    MuslimCoder is offline Senior Member
    Join Date
    Jan 2009
    Posts
    119
    Rep Power
    0

    Default How do you test the fastest methods?

    When I read tutorials about java, I sometime's get to a place where they say that one method is faster than the other. Take for example the ++ x being a bit faster than the x ++.

    How do you test to see which method is faster?

  2. #2
    rjuyal's Avatar
    rjuyal is offline Senior Member
    Join Date
    Mar 2008
    Location
    Delhi, India
    Posts
    194
    Rep Power
    9

    Default

    Simply use

    Java Code:
    long  tim = System.currentTimeMillis();
    	for ( i = 0; i < 30000; i++ ){  
    		//System.out.println(++x);
    // your statements 
    	}
    	System.out.println( System.currentTimeMillis() - tim );


    ----
    BTW, on my machine x++ took 641 millisecs, where as ++x took 609 millisecs.
    i am the future

  3. #3
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    10

    Default ++x vs x++

    MC: it is very easy to test: just use rjuyal's example as a template:
    Java Code:
    start timer (currentTimeMillis())
    loop using ++x
    end loop
    stop timer (currentTimeMillis())
    start timer (currentTimeMillis())
    loop using x++
    end loop
    stop timer (currentTimeMillis())
    Your loop will have to very big for anything to register in the timer.

    Question: why are you asking questions about the speed between different operators ? Are you developing an application that is performance dependent?

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  4. #4
    MuslimCoder is offline Senior Member
    Join Date
    Jan 2009
    Posts
    119
    Rep Power
    0

    Default

    just want to optimize all the applications I make , in the future

  5. #5
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    10

    Default OK...

    Unless your applications are performence dependant, do an extremely lot of intensive cycles or interface with the mechanical real world, I don't think you have to worry about which methods run faster then others. Please see the following post:
    loop performance (no problem, just investigating)
    On my PC I was able to detect a 200ms difference between three methods after 50 million cycles and the results weren't definite.
    You should probably worry more about the way the application is programmed (correct use of methods/OOP, instantiation, etc) than the methods themselves.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. Replies: 1
    Last Post: 12-28-2008, 11:25 AM
  3. Connectivity Test
    By jimhaddon in forum Java Applets
    Replies: 9
    Last Post: 08-16-2008, 04:40 PM
  4. Junit test
    By alice in forum New To Java
    Replies: 1
    Last Post: 06-14-2008, 01:24 AM
  5. Replies: 0
    Last Post: 04-05-2008, 12:38 AM

Posting Permissions

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