java and the time

marshian

New Member
Messages
526
Reaction score
9
Points
0
I'm trying to make a thread which shows the time that had been required for a executing a command...
In other words: it stores the starttime, executes a command, and compares the start time to the current time.
But somewhere, something is going wrong, and the start time is always equal to the end time, even while I know it isn't.
This is my code:
Code:
    public void run() {
        try {
            for (; steps <= endstep; steps++) {
                if (steps == 0) {
                    Main.graphicview.points = points;
                    Main.graphicview.repaint();
                    Thread.sleep(sleep);
                    continue;
                }
                long starttime = Calendar.getInstance().getTimeInMillis();
                Main.statusbarTextLeft.setText(" Berekenen...");
                Thread nextStep = new Thread(new Curve());
                nextStep.start();
                nextStep.join();
                Main.graphicview.points = Curve.points;
                Main.graphicview.repaint();
                long endtime = Calendar.getInstance().getTimeInMillis();
                Main.statusbarTextLeft.setText(" Berekening: "+(endtime-starttime)+" ms");
                Thread.sleep(sleep);
            }
        } catch (InterruptedException ex) {
            System.out.println("Thread interrupted.");
            return;
        }
        if(steps >= endstep) return;
    }

Could anyone please help me?
Why is starttime always equal to endtime?

Thanks,
Marshian
 

konekt

New Member
Messages
100
Reaction score
0
Points
0
Sometimes those commands run faster then is notable.

Your time declarations also occur at the same time:

long starttime = Calendar.getInstance().getTimeInMillis();
Main.statusbarTextLeft.setText(" Berekenen...");
Thread nextStep = new Thread(new Curve());
nextStep.start();
nextStep.join();
Main.graphicview.points = Curve.points;
Main.graphicview.repaint();
long endtime = Calendar.getInstance().getTimeInMillis();
Main.statusbarTextLeft.setText(" Berekening: "+(endtime-starttime)+" ms");

The steps between startime and endtime should take close to no time to execute.
 
Last edited:

leafypiggy

Manager of Pens and Office Supplies
Staff member
Messages
3,819
Reaction score
163
Points
63
...thats not java....
 

leafypiggy

Manager of Pens and Office Supplies
Staff member
Messages
3,819
Reaction score
163
Points
63
That is not a java code...its a snippit....
 
Last edited:

marshian

New Member
Messages
526
Reaction score
9
Points
0
None of the commands in that snippit (since it's not considerred to be code by leafypiggy...) take any notable time to execute, but the Thread nextStep does take some time, and since i use nextStep.join(), the whole execution time will be something...
I tried vTech's solution, and strangely, it works :s
So all I had to do was replace
Code:
                long endtime = Calendar.getInstance().getTimeInMillis();
                Main.statusbarTextLeft.setText(" Berekening: "+(endtime-starttime)+" ms");
with
Code:
                long timediff = Calendar.getInstance().getTimeInMillis() - starttime;
                Main.statusbarTextLeft.setText(" Berekening: "+timediff+" ms");

Thanks for responding

Btw, the thread nextStep had to calculate 8193 points, which all had to be drawn after that, so sometimes commands do take time ;-)

Note: I know I should close and I would close this thread, but how?
 

vTech

New Member
Messages
34
Reaction score
0
Points
0
on ur 1st post, there's a link somewhere near the top of the post called "Thread Tools" click it and then click close thread...
 

marshian

New Member
Messages
526
Reaction score
9
Points
0
I know, but it only has 3 functions:
- show printable version
- email this page
- subscribe to this thread
 

konekt

New Member
Messages
100
Reaction score
0
Points
0
That is not a java code...its a snippit....
:nuts:


Anyway, if you will allow for some guessing as to why that solution worked, I believe it has something to do with the order of the java compiler's execution where the variables were siphoned as a decelerations before the thread execution (i.e. the change made the diff.time an execution as opposed to the declaration so it occurred at a different time then before). This makes me want to look at the process of the java compiler. I could be over thinking things as well, but this is really interesting.
 
Last edited:

marshian

New Member
Messages
526
Reaction score
9
Points
0
:nuts:


Anyway, if you will allow for some guessing as to why that solution worked, I believe it has something to do with the order of the java compiler's execution where the variables were siphoned as a decelerations before the thread execution (i.e. the change made the diff.time an execution as opposed to the declaration so it occurred at a different time then before). This makes me want to look at the process of the java compiler. I could be over thinking things as well, but this is really interesting.

If you'ld find anything, me and probably other people would be very interested. ^^
 

woiwky

New Member
Messages
390
Reaction score
0
Points
0
This is just gonna make it more confusing, but I'm seeing no difference between using endtime-starttime and timediff in my own tests. Both seem to calculate the execution time exactly the same(as would be expected). Furthermore, I've used almost the exact same code myself in many other situations and I've never had any problem with it. It's very curious that the former isn't calculating correctly for you, but the latter does. But if it works, it works.
 
Top