Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4608952
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:55:03+00:00 2026-05-22T00:55:03+00:00

the following code: String str1=asdfavaxzvzxvc; String str2=werwerzsfaasdf; Object c=str1; Object d=str2; System.out.println(c); long time1=System.currentTimeMillis();

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T00:55:04+00:00Added an answer on May 22, 2026 at 12:55 am

    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
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying following code String s1 = ß.cfg; System.out.println (s.toUpperCase()); output I am
Right now I have the following code: String[] values = {str1, str2}; Utils.myMethod(values); I
Consider the following code: #include <stdio.h> int main (void) { char str1[128], str2[128], str3[128];
Consider the following example. String str = new String(); str = "Hello"; System.out.println(str); //Prints
In the following code: using namespace std; //ostream& operator<< (ostream& out,const string & str)
The following code doesn't work: class String{ public: char* str; int* counter; String(){ str
I have the following code: String inputFile = somefile.txt; FileInputStream in = new FileInputStream(inputFile);
currently I have the following code: String select = qry.substring(select .length(),qry2.indexOf( from )); String[]
I have the following code string three() { return three; } void mutate(string& ref)
I have the following code: string acctStatus = account.AccountStatus.ToString(); if (!SettableStatuses().Any(status => status ==

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.