- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在编写一个 FTP 客户端应用程序,并在代码中的两个点使用了 System.nanoTime(),返回的秒数差异为 18,而我的程序只需要 2 秒...检查 onPostExecute 方法中的日志...为什么会发生这种情况以及如何解决?
protected String doInBackground(String... urls)
{
try
{
if(perflag)
return "Not yet";
String ipadd= ip.getText().toString();
BufferedReader br;
int port =Integer.parseInt(portt.getText().toString());
while(true) {
socket = new Socket(ipadd, port);
//Button conn=(Button) findViewById(R.id.connectb);
if(startflag)
{
starttime=System.nanoTime();
startflag=false;
}
//conn.setEnabled(false);
br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedOutputStream bo = new BufferedOutputStream(socket.getOutputStream());
PrintWriter pw = new PrintWriter(bo, true);
file = br.readLine();
if (file.equals("Finished"))
{
// socket.close();
Log.i("stat","above sock");
// Thread.sleep(1000);
//socket= new Socket(ipadd,port);
//br= new BufferedReader(new InputStreamReader(socket.getInputStream()));
datas=br.readLine();
data=Double.parseDouble(datas);
Log.i("stat",datas);
br.close();
socket.close();
finflag=true;
break;
}
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);
File f = new File(path, "/" + file);
byte[] buff = new byte[1024];
int len;
int i=0;
while(f.exists())
{
f= new File(path,"/"+"("+i+")"+file);
i++;
}
pw.println("done");
DataInputStream is = new DataInputStream(socket.getInputStream());
FileOutputStream fos = new FileOutputStream(f);
publishProgress(file);
while ((len = is.read(buff)) != -1) {
fos.write(buff, 0, len);
}
publishProgress(file);
}
}catch(NumberFormatException nfe)
{
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(MainActivity.this,"Enter a proper IP and port!",Toast.LENGTH_LONG).show();
start();
}
});
nfe.printStackTrace();
}
catch(java.net.UnknownHostException un)
{
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(MainActivity.this,"Enter a proper IP and port!",Toast.LENGTH_LONG).show();
start();
}
});
//Toast.makeText(MainActivity.this,"Enter a proper IP and port!",Toast.LENGTH_LONG).show();
un.printStackTrace();
}
catch(java.net.NoRouteToHostException no)
{
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(MainActivity.this,"There is no active server at the specified IP and port!",Toast.LENGTH_LONG).show();
start();
}
});
no.printStackTrace();
}
catch(Exception e)
{
Log.i("error",e.getMessage());
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(MainActivity.this, "Error occurred!Try checking storage permissions or connection.", Toast.LENGTH_LONG).show();
start();
}
});
e.printStackTrace();
}
endtime=System.nanoTime();
return "Not Yet";
}
protected void onProgressUpdate(String... para)
{
super.onProgressUpdate(para);
trans.setText("Transfering file: "+para[0]);
}
protected void onPostExecute(String as) {
start();
if(finflag)
{
startflag=true;
Log.i("start",String.valueOf(starttime));
Log.i("end",String.valueOf(endtime));
totaltime=endtime-starttime;
Log.i("total",String.valueOf(totaltime));
//totaltime/=100000000;
double time=totaltime/1000000000;
Log.i("time in secs",String.valueOf(time));
double rate= (double)(data/time);
Toast.makeText(MainActivity.this,"Transfer successful!",Toast.LENGTH_LONG).show();
Toast.makeText(MainActivity.this,"Average transfer rate: "+rate+"MBps",Toast.LENGTH_LONG).show();
finflag=false;
}
}
最佳答案
进行整数除法来获取秒数没有什么意义,因为这只会返回整秒;您不需要整秒使用nanoTime
。变化:
double time=totaltime/1000000000;
至
double time=totaltime/1000000000d; // (or totaltime/1000000000.0)
但是你使用的除数没问题;纳米为 10-9。但是,我在您的代码中看到了一条注释掉的行,就在您的分区上方:
//totaltime /= 100000000;
double time=totaltime/1000000000;
在那一行中,你的除数只有 8 个零,而不是 9 个零。您确定您发布的“18”秒来自 double time=totaltime/1000000000;
而不是来自 totaltime/= 100000000;
吗?因为如果除数中的 0 太少,则 1.8 秒的实际时间看起来就像 18 秒。
正如评论者所提到的,问题实际上是魔法常数,它很容易得到错误的零个数。
以下是解决此问题的几种方法:
totaltime/1_000_000_000d
,以便更清楚地看到您使用了多少个零。totaltime/1e9
TimeUnit.NANOSECONDS.toSeconds(totaltime)
(它会向下舍入到 1
秒)。但是,您可以说 totaltime/(double) TimeUnit.SECONDS.toNanos(1)
关于java - system.nanoTime() 错误吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56511072/
大家好,我有一段代码如下所示: public class Test { public static void main(String args[]) { long a = Sy
我做了这样的基准: for (int i = 0; i < 1000 * 1000; ++i) { long var = System.nanoTime(); } 在我的jdk6.0电脑上需要
我正在做一个项目,需要在 999 个随机 double 组中实现冒泡排序、希尔排序和快速排序算法。 我需要测量每个算法运行所需的时间。 我正在使用 System.nanoTime() 来测量每个算法的
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
我有一个线程将数据插入队列,另一个线程从队列中读取数据并进行处理。我想检查数据在处理之前在队列中的停留时间。 我在第一个线程推送之前在数据中添加了一个时间参数(使用 System.nanoTime()
可以使用 Clock 模拟调用,例如 System.currentTimeMillis() 使用 Clock.millis() 并注入(inject) Clock 的模拟实现。 是否有类似的方法可以轻
我想以纳秒为单位计算两个事件之间耗时。为此,我可以使用 System.nanoTime()如前所述 here .问题是这两个事件发生在不同的线程中。 自 nanoTime()不返回绝对时间戳,而只能用
我想做与下面这段代码相同的操作,但使用 System.nanoTime()反而 new Timer().schedule(new TimerTask() {
我正在编写一个 FTP 客户端应用程序,并在代码中的两个点使用了 System.nanoTime(),返回的秒数差异为 18,而我的程序只需要 2 秒...检查 onPostExecute 方法中的日
我目前正在使用 Java 编写多线程程序。在某些时候,不同的线程会记录它们的操作,我正在使用 nanoTime为了这。每个线程都有自己的日志文件,最后我将它们合并并根据时间 (nanoTime) 对它
我在我的应用程序中使用 system.nanoTime 并计算相对于开始时间的时间。 我的应用程序运行得很好,直到我将其复制到一台新计算机上,在它上纳米时间给了我较慢的值。 我编写了一个简单的应用程序
这不是我第一次遇到编程语言中的时钟时间问题。基本上,我通过在 while 循环中调用函数来测量函数的运行速度。问题是,由于某种原因,while 循环运行的时间越长,耗时就越短。谁能解释一下吗?代码如下
我使用以下代码来评估我的方法/操作的性能- 问题:由于这是贯穿代码的,是否应将其从生产代码中删除以防止降低应用性能。 在我们的一次讨论中,有人指出 Linux(时区)上的一些时间调用会执行 IO 以查
在 gemfire 上,我有一个区域 ABC,并且在该区域内有一个 long 类型的列/字段。在此字段中,我将值设置为 System.nanoTime()。我想获取该区域 24 小时前的所有记录。我怎
我正在编写一个使用 System.nanotime 的 android 计时器应用程序。问题是它给了我低估的结果和负数。 redVal、blueVal 和 greenVal 在相机的每一帧上更新。 结
我正在开发一个 2d java 游戏,但我遇到了计时器问题。这是我的游戏循环: void gameLoop(isRunning){ .... doStuff(); .... } 我在循环中有一个像这样
我正在关注这个jmh 测试http://hg.openjdk.java.net/code-tools/jmh/file/57623b7f64e6/jmh-core-benchmarks/src/mai
我有一个很长的字符串,其模式为 最后,我试图测试一些函数调用的性能,所以我进行了以下测试以试图找出答案......但我认为我可能错误地使用了 nanoTime?因为无论我如何交换顺序,结果都没有意义.
我使用变量 time_of_last_call 作为我的时间起点;因为 nanoTime() 可能会给出负值,所以我不能使用 0 作为我的时间起点来初始化 time_of_last_call。如果我用
我有一个多线程 Java 程序,可以在几秒钟内创建数百个临时文件。这些文件位于 /tmp 中,并使用 System.nanoTime() 命名。 是否保证文件名是唯一的? 最佳答案 不,不能保证每次调
我是一名优秀的程序员,十分优秀!