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 7509223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:50:31+00:00 2026-05-29T22:50:31+00:00

Given the following pieces of code, which one is more efficient? The real method

  • 0

Given the following pieces of code, which one is more efficient? The real method returnSomething() can also return 0 in reality so try/catch is needed.

//piece one
long sleepTime = 200;
try{ sleepTime /= returnSomething();}
catch(Exception e){sleepTime = 200;}
private int returnSomething(){
   return 1;
}

              //or

//piece two
long sleepTime = 200;
if(returnSomething() == 3){sleepTime = 67;}
else if(returnSomething() == 2){sleepTime = 100;}
else if(returnSomething() == 1){sleepTime = 200;}
private int returnSomething(){
   return 1;
}

I was trying to figure out which piece of code is more efficient in terms of processor usage, they bot do the same thing. I would like to know if the code I wrote for testing is fit for the purpose or whether I can do other kind of testing on the code. My findings show that piece 2 is 9 times more efficient (9 times less time to execute) even though it uses hard-code if statements and the last if statement is always executed.

Full working program

public class CodePerformanceTester
{
    public static void main(String[] args){
        CodePerformanceTester tester = new CodePerformanceTester();
        tester.start();
    }

    public void start(){
       double start = System.currentTimeMillis();
       long sleepTime = 200;
       for(int i=0; i<10000000; i++){

           //uncoment here the two lines below
           //try{ sleepTime /= returnSomething();}
           //catch(Exception e){sleepTime = 200;}

           //coment the IF STATEMENTS when above code uncomented
           if(returnSomething() == 3){sleepTime = 67;}
           else if(returnSomething() == 2){sleepTime = 100;}
           else if(returnSomething() == 1){sleepTime = 200;}
       }
       double end = System.currentTimeMillis();
       System.out.println("Execution time for 10 million iteration was "+(end-start)+" ms.");
    }


    private int returnSomething(){
       return 1;
    }
}
  • 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-29T22:50:33+00:00Added an answer on May 29, 2026 at 10:50 pm

    The first piece is probably slower because dividing is more expensive than comparing values.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following piece of code which attempts to determine whether a given
Given the following piece of code: template<typename T> class MyContainer { typedef T value_type;
Given the following code (that doesn't work): while True: # Snip: print out current
I have the following piece of code which doesn't compile when I try to
The following piece of code was given to us from our instructor so we
consider the following two pieces of code: public static Time Parse(string value) { string
I'm trying to set up a parser which, given a value, can assign it
I have the following code written in C++ to extract a given range of
I have the following piece of code which is being run from a click
Given following Ruby statements: (Read input and store each word in array removing spaces

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.