1

I have two methods that I wish to profile in netbeans

Here is there code

   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;


    }

How ever netbeans returns 0ms for both the methods !! I ran the netbeans profiler for the whole project and the snap shot says that the self time of each of these methods is 0ms.

I am calling these two methods from main using a loop 1000 times. If there execution time is so significantly less that they cannot be expressed in MS is there a way to express it in nano seconds ? I would be running these methods a million times per image frame which is 30 million times a second. I need to profile and choose the best method very badly.

Aditya
  • 1,240
  • 2
  • 14
  • 38
  • Will a difference of less than a millisecond really matter? (answer: no) – tckmn Jan 02 '14 at 20:44
  • (Side remark: your first method can be written in the one line `return (a - b) > 10`, and you can replace the `if` in your second method with `return a == b`. – Louis Wasserman Jan 02 '14 at 20:53
  • Do you have a clock with a second hand? Don't run them 10^3 times. Run them 10^9 times and see how many seconds they take. That tells you how many nanoseconds one call takes. (1 ms = 10^6 ns, which is probably why you're seeing 0ms.) By the way, it doesn't look like those two functions do the same thing. – Mike Dunlavey Jan 02 '14 at 22:32

3 Answers3

2

I need to profile and choose the best method very badly.

Construct a micro-benchmark: How do I write a correct micro-benchmark in Java? (it's harder than it may seem).

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
1

Here, for your convenience:

@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);
  }
}

Result:

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

Which means that, at least on my computer, you could achieve 288 million calls per second for the slower method (compare2).

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
0

I mainly see quick bit manipulations in your example code so 0 ms is not astonishing. You can try System.nanoTime() to measure the relative duration for a pretty long cycle of loops.

But: Why do you care about optimization/performance of such quick code???

Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126
  • This is just part of the method there is some other stuff in it which brings the execution time to ms. but when I saw 0ms I panicked I thought I was not doing it properly so made this post. – Aditya Jan 03 '14 at 07:26