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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:14:26+00:00 2026-06-11T05:14:26+00:00

Due to the benchmarking done in other threads (cf. https://stackoverflow.com/a/397617/1408611 ) it was shown

  • 0

Due to the benchmarking done in other threads (cf. https://stackoverflow.com/a/397617/1408611) it was shown that instanceof in Java 6 is actually quite fast. How is this achieved?

I know that for single inheritance, the fastest idea is having some nested interval encoding where each class maintains a [low,high] interval and an instanceof is simply an interval inclusion test, i.e. 2 integer comparisons. But how is it made for interfaces (as interval inclusion only works for single inheritance)? And how is class loading handled? Loading new subclasses means that a lot of intervals have to be adjusted.

  • 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-06-11T05:14:28+00:00Added an answer on June 11, 2026 at 5:14 am

    AFAIK each class knows all the classes it extends and interfaces it implements. These could be stored in a hash set giving O(1) lookup time.

    When code often takes the same branch, the cost can be almost eliminated as the CPU can execute the code in the branch before it has determined whether it should take the branch making the cost next to nothing.

    As the micro-benchmark was performed 4 years ago, I expect the latest CPUs and JVMs to be much faster.

    public static void main(String... args) {
        Object[] doubles = new Object[100000];
        Arrays.fill(doubles, 0.0);
        doubles[100] = null;
        doubles[1000] = null;
        for (int i = 0; i < 6; i++) {
            testSameClass(doubles);
            testSuperClass(doubles);
            testInterface(doubles);
        }
    }
    
    private static int testSameClass(Object[] doubles) {
        long start = System.nanoTime();
        int count = 0;
        for (Object d : doubles) {
            if (d instanceof Double)
                count++;
        }
        long time = System.nanoTime() - start;
        System.out.printf("instanceof Double took an average of %.1f ns%n", 1.0 * time / doubles.length);
        return count;
    }
    
    private static int testSuperClass(Object[] doubles) {
        long start = System.nanoTime();
        int count = 0;
        for (Object d : doubles) {
            if (d instanceof Number)
                count++;
        }
        long time = System.nanoTime() - start;
        System.out.printf("instanceof Number took an average of %.1f ns%n", 1.0 * time / doubles.length);
        return count;
    }
    
    private static int testInterface(Object[] doubles) {
        long start = System.nanoTime();
        int count = 0;
        for (Object d : doubles) {
            if (d instanceof Serializable)
                count++;
        }
        long time = System.nanoTime() - start;
        System.out.printf("instanceof Serializable took an average of %.1f ns%n", 1.0 * time / doubles.length);
        return count;
    }
    

    finally prints

    instanceof Double took an average of 1.3 ns
    instanceof Number took an average of 1.3 ns
    instanceof Serializable took an average of 1.3 ns
    

    if I change the “doubles” with

        for(int i=0;i<doubles.length;i+=2)
            doubles[i] = "";
    

    I get

    instanceof Double took an average of 1.3 ns
    instanceof Number took an average of 1.6 ns
    instanceof Serializable took an average of 2.2 ns
    

    Note: If I change

    if (d instanceof Double)
    

    to

    if (d != null && d.getClass() == Double.class)
    

    the performance was the same.

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

Sidebar

Related Questions

Due to a number of constraints that I won't get into, I have to
Due to weird domain/subdomain cookie issues that I'm getting, I'd like to know how
Due to Zend rewriting URL's to module/controller/action, its reporting that images are coming through
Due to quota restrictions I really need to build a project that I'm working
Due to Bundle install choking on libv8 dependency from theRubyRacer, JSON and other gems
Due to a bug that was fixed in C# 4, the following program prints
Due to validation markup and some other reasons I disable the list-style-type css property
Due to repetitive errors with one of our Java applications: Engine engine_0: Error in
EDIT 2: Confirmed that my performance problems were due to the static function call
Due to the fact that my setups keeps growing I'd like to use some

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.