Computer Science Canada

when i try to find my run time it is always zero

Author:  B1u3z [ Tue Feb 14, 2012 2:51 pm ]
Post subject:  when i try to find my run time it is always zero

long time ;
time = System.currentTimeMillis();
( rest of program)
time = System.currentTimeMillis() - time ;
JOptionPane.showMessageDialog (null, time+"ms","time", JOptionPane.WARNING_MESSAGE);

i am trying to find my run time of my program but every time i run it states that it took 0ms. can i get some help on ho to fix this

Author:  Zren [ Tue Feb 14, 2012 3:12 pm ]
Post subject:  RE:when i try to find my run time it is always zero

What does the rest of your program do? The code is fine. Your program is probably just not resource intensive enough to have those two sections of code to happen in a different clock cycle.

If you want to confirm this, dump a Thread.sleep in there with it's appropritate exception handling.

Java:


public class Test {
        public static void main(String[] args) {
                long time;
                time = System.currentTimeMillis();
                try {
                        Thread.sleep(1000);
                } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                time = System.currentTimeMillis() - time;
                JOptionPane.showMessageDialog(null, time + "ms", "time", JOptionPane.WARNING_MESSAGE);
        }
}

Author:  mirhagk [ Tue Feb 14, 2012 11:08 pm ]
Post subject:  RE:when i try to find my run time it is always zero

Your computer runs in gigahertz. That's trillions of operations every second, which is billions every million second. SO unless your code is a billion operations it won't show up.

Author:  Insectoid [ Wed Feb 15, 2012 7:52 am ]
Post subject:  Re: RE:when i try to find my run time it is always zero

mirhagk @ Tue Feb 14, 2012 11:08 pm wrote:
Your computer runs in gigahertz. That's trillions of operations every second, which is billions every million second. SO unless your code is a billion operations it won't show up.


Quote:
Thread.sleep(1000);

Author:  B1u3z [ Wed Feb 15, 2012 12:12 pm ]
Post subject:  Re: when i try to find my run time it is always zero

thanks for the help

Author:  Insectoid [ Wed Feb 15, 2012 12:40 pm ]
Post subject:  RE:when i try to find my run time it is always zero

Oh, my bad. I thought Zren was OP (as I didn't check the username) and only read the title and code snippet.


: