I need the time in milliseconds for what could be a large volume of transactions, so I want something that is correct, and fast. Will the following work and do the job best? :
iMilli := int((time.Nanoseconds() % 1e6) / 1e3)
TIA
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
EDIT: Since this answer was first written, escape analysis code has been added to the Go compilers. This allows the compiler to avoid unnecessary allocations in certain situations, including (probably) the one described below. With the latest weeklies, therefore, it may be just as good to use a more straightforward call to time.Nanoseconds(). Please do your own profiling.
Most of the time functions cause a heap allocation (that then subsequently needs to be collected, causing a pause in your application). So if you’re looking up the time frequently, which it sounds like you are, you’ll need to use syscall.Gettimeofday() directly (it’s the function that the other time functions end up calling anyway). See the discussion here for more information:
http://groups.google.com/group/golang-nuts/browse_thread/thread/f2209022f43efcca?pli=1
The solution I’m using is to pre-allocate a
tv syscall.Timeval, and each time through my inner loop I do this:You can then get the milliseconds with:
I’ve found this to perform a lot better than calling time.Nanoseconds() or one of the other higher-level time functions.