简体   繁体   English

使用currentTimeMillis测量经过时间返回0

[英]Using currentTimeMillis to measure elapsed time is returning 0

I am somewhat new to Java/Android programming and I am just trying to do a simple time measure to calculate the elapsed time of a method that I am calling. 我是Java / Android编程的新手,我只是想尝试做一个简单的时间量度来计算我正在调用的方法的经过时间。

Right now my code looks like this: 现在,我的代码如下所示:

        startTime = System.currentTimeMillis();
        java_doubleinterp(args, ans);
        endTime = System.currentTimeMillis();
        elapsed = endTime - startTime;
        setTextview(ans);

The method java_doubleinterp contains a for loop and it is purely mathematical and working properly. 方法java_doubleinterp包含一个for循环,它纯粹是数学的并且可以正常工作。 The input is args and the output is ans . 输入为args ,输出为ans I am not returning anything because I am trying to compare the performance of this to a similar function implemented using C/C++ (NDK) that was generated automatically with MatLab and has this structure. 我没有返回任何内容,因为我试图将其性能与使用MatLab自动生成并具有此结构的C / C ++(NDK)实现的类似功能进行比较。

For the C/C++ function, I am trying to measure the time in the same way and getting the same error. 对于C / C ++函数,我试图以相同的方式测量时间并得到相同的错误。

Later in the code I have this: 稍后在代码中我有这个:

Toast elapsedTime = Toast.makeText(getApplicationContext(), 
            "Elapsed time for " + lang + " code: \n" + (elapsed*1000)+ "\u03BCs", 
            Toast.LENGTH_LONG);

where lang is just a string. lang只是一个字符串。

This toast is returning me that the elapsed time is 0us (microseconds). 这个吐司让我回想起经过的时间是0us(微秒)。

I displayed the startTime and endTime when trying to debug this problem and both of them show the same value indeed. 尝试调试此问题时,我显示了startTime和endTime,并且两者确实显示了相同的值。 Why is this happening? 为什么会这样呢?


Obs: I tried using nanoTime() to get the times and have better precision but the results were: Obs:我尝试使用nanoTime()来获取时间并具有更好的精度,但是结果是:

  • Emulator: around 50us 模拟器:约50us
  • Phone (Nexus 4) : either 0us or 30us [doesn't make sense] 电话(Nexus 4):0us或30us [没有意义]

EDIT: 编辑:

I made some changes in my code and it is looking like this now for the java method: 我对代码进行了一些更改,现在对于java方法,它看起来像这样:

startTime = System.nanoTime();
java_doubleinterp(args, ans);
setTextview(ans);
endTime = System.nanoTime();
elapsed = endTime - startTime;

And this is the part for the c function (JNI): 这是c函数(JNI)的一部分:

startTime = System.nanoTime();
doubleinterp(args, ans);
setTextview(ans);
endTime = System.nanoTime();
elapsed = endTime - startTime;

They are now working fine. 他们现在工作正常。 Since the setTextview is the same method for the two parts of code, the absolute execution time may be wrong but the relative execution time (between java implementation and JNI implementation) is ok. 由于setTextview对于代码的两个部分是相同的方法,因此绝对执行时间可能是错误的,但是相对执行时间(在Java实现和JNI实现之间)可以。

Now I am getting average execution times that make more sense: 现在,我得到了更有意义的平均执行时间:

  • Emulator: 3.75 ms (Java) / 6.59 ms (JNI) 仿真器:3.75毫秒(Java)/ 6.59毫秒(JNI)
  • Nexus 4: 1.11ms (Java) / 0.82 ms (JNI) Nexus 4:1.11毫秒(Java)/ 0.82毫秒(JNI)

您正在尝试评估虚拟例程的性能:可能是编译器只是使整个过程消失,这就是为什么您得到0微秒的原因。

确保变量startTime和endTime为Long

It seems that your routine finishes very fast so that the difference between your start time and end is 0. When we measure the performance, as far as I know, nanoTime() is much better than currentTimeMillis(). 看来您的例程完成得非常快,因此开始时间和结束时间之间的差为0。据我所知,当我们测量性能时,nanoTime()比currentTimeMillis()好得多。 Try to increase the size of input 尝试增加输入的大小

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM