Quantcast

Maximum PC

It is currently Wed Apr 16, 2014 9:52 pm

All times are UTC - 8 hours




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Is there a better way to obtain timestamps in Java?
PostPosted: Fri Sep 17, 2004 9:58 am 
Little Foot
Little Foot

Joined: Sun Jun 27, 2004 7:11 am
Posts: 124
Location: 127.0.0.1
Is there a better way to obtain timestamps in Java? I need to do some performance test in Java, and am using

Calendar.getInstance().getTimeInMillis() to grab timestamps. Unfortunately, this method seems to have some overhead. So I am just wondering if there is a better way of obtaining timestamps in Java than using Calendar. Thanks.


Top
  Profile  
 
 Post subject:
PostPosted: Fri Sep 17, 2004 5:40 pm 
Team Member Top 100
Team Member Top 100

Joined: Fri Sep 17, 2004 5:35 pm
Posts: 1176
The most accurate way to gain time information on an x86 system is to use the RDTSC assembly instruction. This will give a 64-bit value containing the current tick count. This isn't supported on all processors, but I've found it to be useful.

However, I do not program in Java, and I do not know if there is a way to use assembly in Java. Even if you cannot, though, you could always write a dll for use with your program.

You can then get the processor speed and you can see how many ticks to one nanosecond, one milisecond, etc.

Here's a reference:
http://www.math.uwaterloo.ca/~jamuir/rdtscpm1.pdf


Top
  Profile  
 
 Post subject:
PostPosted: Fri Sep 17, 2004 6:25 pm 
Little Foot
Little Foot

Joined: Sun Jun 27, 2004 7:11 am
Posts: 124
Location: 127.0.0.1
hi kybo,

really appreciate your reply. Unfortunately I cannot use assembly instructions. Although at the moment, our test bed is an x86 machine, but later on, we will move on to some different architecture (could be solaris, but not sure yet), and we probably have to do another performance test there. That is another reason why we used Java in the first place.

If we write an DLL, then yes, we can always use the JNI, but I don't know how much overhead would be involved in that. It may be faster, but could also be much slower than using the Calendar method.

Any how, I really appreciate your input, especially the link to the PDF. Maybe not this time, but sooner or later, I will need to use the RDTSC instruction.

thanks!

eman


Top
  Profile  
 
 Post subject:
PostPosted: Fri Sep 17, 2004 7:58 pm 
Team Member Top 100
Team Member Top 100

Joined: Fri Sep 17, 2004 5:35 pm
Posts: 1176
Then can you put the function you wish to benchmark in a loop (to execute it say, a thousand times), and then divide by the milisecond timestamp you have?


Top
  Profile  
 
 Post subject:
PostPosted: Fri Sep 17, 2004 8:24 pm 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24218
Location: Granite Heaven
Does the java.util.Date class not do what you need?

http://java.sun.com/j2se/1.4.2/docs/api/


Top
  Profile  
 
 Post subject: Re: Is there a better way to obtain timestamps in Java?
PostPosted: Sat Sep 18, 2004 1:43 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
emanresu wrote:
Is there a better way to obtain timestamps in Java? I need to do some performance test in Java, and am using

Calendar.getInstance().getTimeInMillis() to grab timestamps. Unfortunately, this method seems to have some overhead. So I am just wondering if there is a better way of obtaining timestamps in Java than using Calendar. Thanks.


Not sure what you mean by overhead, but you can use System.currentTimeMillis() to do this type of thing.

Code:
public class TimeStamp {
    public static void main(String[] args) {

        //start timer
        long time1 = System.currentTimeMillis();
       
        //do something

        //end timer
        long time2 = System.currentTimeMillis();
        System.out.println("time2:  " + time2);
        System.out.println("time1:  " + time1);
        System.out.println("diff:   " + (time2 - time1));
        System.exit(0);
    }
}


Top
  Profile  
 
 Post subject:
PostPosted: Fri Sep 24, 2004 3:37 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
I can't believe I forgot to mention it. Java 1.5 has a more accurate timer now. I haven't used it, but you might want to check it out.


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group