计算时差(以毫秒为单位) [英] Calculating time difference in Milliseconds

查看:84
本文介绍了计算时差(以毫秒为单位)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过传递ipAddress来调用方法,该方法将返回ipAddress的位置,例如国家/地区,城市等.因此,我试图查看每个调用花费了多少时间.因此,我在调用方法之前设置了start_time,在调用之后设置了end_time. 所以有时我得到的差异为0 .并且resp包含有效的响应.

I am making a call to a method by passing ipAddress and it will return back the location of ipAddress like Country, City, etc etc. So I was trying to see how much time it is taking for each call. So I set the start_time before making call to method and end_time after making a call. So sometimes I get difference as 0. And resp contains the valid response.

long start_time = System.currentTimeMillis();
resp = GeoLocationService.getLocationIp(ipAddress);
long end_time = System.currentTimeMillis();
long difference = end_time-start_time;

因此,这意味着有时需要0毫秒才能获得响应.任何建议将不胜感激.

So that means sometimes it is taking 0 ms to get the response back. Any suggestions will be appreciated.

推荐答案

尝试一下

long start_time = System.nanoTime();
resp = GeoLocationService.getLocationByIp(ipAddress);
long end_time = System.nanoTime();
double difference = (end_time - start_time) / 1e6;

这篇关于计算时差(以毫秒为单位)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆