方法在netbeans profiler中占用0ms

时间:2014-01-02 20:44:06

标签: java netbeans profiling

我想在netbeans中分析两种方法

这里有代码

   public static boolean compare1(int a,int b){

        if((a-b)>10)
            return true;
        else
            return false;

    }
    public static boolean compare2(int a,int b){

        a = ((a&0xf00000)>>12)+((a&0xf000)>>8)+((a&0xf0)>>4);
        b = ((b&0xf00000)>>12)+((b&0xf000)>>8)+((b&0xf0)>>4);
        if(a==b)
            return true;
        else 
            return false;


    }

netbeans如何为这两种方法返回0ms !!我为整个项目运行了netbeans探测器,快照表示每种方法的自我时间都是0ms。

我使用循环1000次从main调用这两个方法。如果执行时间显着减少,以至于无法在MS中表达它是否有办法在纳秒内表达它?我会在每个图像帧上运行这些方法一百万次,每秒3000万次。我需要剖析并选择最好的方法。

3 个答案:

答案 0 :(得分:2)

  

我需要剖析并选择最好的方法。

构建微观基准:How do I write a correct micro-benchmark in Java?(它看起来比它看起来更难)。

答案 1 :(得分:1)

在这里,为方便起见:

@OutputTimeUnit(TimeUnit.MICROSECONDS)
@BenchmarkMode(Mode.Throughput)
@Warmup(iterations = 3, time = 1)
@Measurement(iterations = 3, time = 1)
@State(Scope.Thread)
@Fork(3)
public class Comparison
{
  int a, b;
  final Random rnd = new Random();

  @Setup(Level.Iteration)
  public void setup() { a = rnd.nextInt(); b = rnd.nextInt(); }

  @GenerateMicroBenchmark
  public boolean testCompare1() { return compare1(a, b); }

  @GenerateMicroBenchmark
  public boolean testCompare2() { return compare2(a, b); }

  static boolean compare1(int a, int b) { return a - b > 10; }
  static boolean compare2(int a, int b){
    return ((a&0xf00000)>>12)+((a&0xf000)>>8)+((a&0xf0)>>4) ==
           ((b&0xf00000)>>12)+((b&0xf000)>>8)+((b&0xf0)>>4);
  }
}

结果:

Benchmark        Mode Thr    Cnt  Sec         Mean   Mean error    Units
testCompare1    thrpt   1      9    1      529.178        5.925 ops/usec
testCompare2    thrpt   1      9    1      288.288        4.058 ops/usec

这意味着,至少在我的计算机上,对于较慢的方法,你可以达到每秒2.88亿次调用(compare2)。

答案 2 :(得分:0)

我主要在你的示例代码中看到快速位操作,因此0 ms并不令人惊讶。您可以尝试System.nanoTime()来衡量相当长的循环周期的相对持续时间。

但是:你为什么关心这种快速代码的优化/性能?

相关问题