Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output timing problem

Tags:

java

timing

the following code:

String str1="asdfavaxzvzxvc";
String str2="werwerzsfaasdf";
Object c=str1;
Object d=str2;
System.out.println(c);
long time1=System.currentTimeMillis();
for(int i=0;i<1000000000;i++){
    if(c.equals(d)){
        //System.out.println("asfasdfasdf"); // line 9
    }
}
long time2=System.currentTimeMillis();
System.out.println("time taken in this is "+(time2-time1));

When I uncomment the line 9, that is let print if condition is true, though never it is going to happen since both object are not equal , then it takes 5000+ milli-seconds, and to my surprise by just commenting it takes only 5 milli-seconds, I am not getting the reason, why it takes so much time if it is not commented, since it's never going to get executed...

Is this some sort of branch prediction effect ? or any sort of compiler optimization

like image 506
peeyush Avatar asked May 12 '11 17:05

peeyush


People also ask

What are the timing issues in VLSI?

With the advances in VLSI design, chip timing is becoming dominated by interconnect delays rather than macro performances. For many existing large computer circuits, the interconnect delays already account for more than a half of the clock cycle, and the portion of propagation time in the cycle continues to grow.

What are the timing issues in synchronous circuit design?

TIMING ISSUES IN DIGITAL CIRCUITS: SYNCHRONOUS DESIGN The presentation includes skew and jtter and its sources, Clock-Distribution Techniques, Latch-Based Clocking. The presentation includes skew and jtter and its sources, Clock-Distribution Techniques, Latch-Based Clocking.

How do you reduce setup time violations?

Since setup time violation can be solved by decreasing the data path logic delay, using a flop with a smaller clock-q delay for launch flip-flop will ease timing requirement. Using a faster cell for launch flip-flop: Flip-flop comes with various threshold voltage (VT).


1 Answers

The compiler optimizes away dead code — in this case, the entire loop is removed. This might be done by the bytecode compiler (e.g. javac) or, more likely, by HotSpot's JIT.

Why does it still take a whopping 5 ms for this to execute? It doesn't necessarily take all that long. Instead, you might be hitting the resolution limit on System.currentTimeMillis(). Try it with System.nanoTime() instead. FWIW, using nanoTime() agrees with currentTimeMillis() on my Windows system.

You might be interested in reading How do I write a correct micro-benchmark in Java? and Is stopwatch benchmarking acceptable?

Further reading

  • White Paper: The Java HotSpot Performance Engine Architecture
  • HotSpot Home Page
like image 53
Matt Ball Avatar answered Sep 21 '22 07:09

Matt Ball