How fast is instanceof?

How expensive is an instanceof operation? Is it in the order of simple arithmetic, or of reflection? Does it vary much?

How expensive is an instanceof operation? Is it in
the order of simple arithmetic, or of reflection?
Does it vary much?Between the two, and yes. It's rarely going to be as slow as reflection, but when you get a "hit" it's practically as fast as arithmetic.
So performance is rarely a reason to avoid "sanity tests" in code (remember, instanceof is essentially a check for != null as well as the type of the object).
Try the following code snippet:
import java.util.*;
public class iof
   public static void main(String[] argv)
      int c = 1000000;
      List content = new ArrayList(c);
      for(int i = 0; i < c; i++ )
         content.add(new String("" + i));
      boolean ignoreState = false;
      Iterator i;
      long start = new Date().getTime();
      i = content.iterator();
      while(i.hasNext())
         Object o = i.next();
      // Instance of (miss)
         ignoreState = (o instanceof List);
      long middle1 = new Date().getTime();
      i = content.iterator();
      while(i.hasNext())
         Object o = i.next();
      // Instance of (hit)
         ignoreState = (o instanceof String);
      long middle2 = new Date().getTime();
      i = content.iterator();
      while(i.hasNext())
         Object o = i.next();
      // Instance of (arithmetic)
         ignoreState = ((2 + 2) > 3);
      long middle3 = new Date().getTime();
      i = content.iterator();
      while(i.hasNext())
         Object o = i.next();
         ignoreState = ( o.getClass().getName().equals(STRINGNAME) );
      long end = new Date().getTime();
      System.out.println("Instanceof (Miss) : " + (middle1 - start) + "ms");
      System.out.println("Instanceof (Hit)  : " + (middle2 - middle1) + "ms");
      System.out.println("Arithmetic        : " + (middle3 - middle2) + "ms");
      System.out.println("Reflection        : " + (end - middle3) + "ms");
   private static String STRINGNAME = "java.lang.String";
}

Similar Messages

  • How fast does my interent connection have to be for Apple TV

    How fast does my internet have to be for Apple TV to work? I currently have 20 bps

    On edit -- never mind - you just got the answer from someone!
    I don't know the answer Russ, but you may want to correct the title of your post as it says Facebook and not FaceTime -- someone who has information about the answer may not open the post to see the actual question. The title currently is:
    how fast does the internet connection need to be for Facebook

  • After getting iOS 6.1.2 wifi inactive. How fast can solve this issue in UK service center?

    For Iphone 4S - After updating to iOS 6.1.2 wifi became inactive(grayved - i can't turn it on).
    Phone still on warranty. I've tried everything - recover, setings resets etc.
    How fast can solve this issue in UK service center?
    I will be in London for 2 days, and i've bought my iphone there.
    Will they be able to change my phone in these 2 days?
    Thanks.

    no one can say for sure, it depends on how busy they are, you should try to make an appt. in the meantime, have you tried the help from the following apple documents:
    http://www.apple.com/support/iphone/wifi/
    http://support.apple.com/kb/ts1398

  • TS3297 I am unable to redeem a digital copy of a movie i purchased. itunes keeps saying timed out, try again, no matter how fast I enter the redeem code. itunes help/support is rubbish.

    I have bought a Movie (20th century fox dist) with a digital copy i can download to itunes. everytime i try to redeem the code, itunes say's "session timed out, try again", no matter how fast I try. itunes help/support is no help. anybody had a similar problem and resolved?

    I agree. I don't rely on iCloud as a backup, that is what I have my portable hard drive for. Its 500 GB so I can hold my entire iTunes library several times over on it. I have all my movies on my hard drive, but somehow "The Mist" got deleted off of my hard drive, so I figured "Well, the option to redownload an already purchased movie is available through iCloud, I'll just do that!"
    And permissions and download availability have nothing to do with it, the movie's still there, it still allows me to redownload it. The only problem is when I click download, I get that message.
    And nobody else uses my computer, but I do have multiple accounts authorized on it. Even still though, I am attepmpting to download it through the account I purchased it under. :/

  • PCI 6110 Digital I/O, How to and How fast?

    The max the board can run is 5MS/s. There are Digital and Analog I/Os. I want to run Analog and Digital I/O with different speeds, analog @ 1.25MS/s and Digital at ? (don't know how fast it can go). I think I can achieve it by using PFI pins, but I am not sure. Could those be used as timing signals for Digital I/O?
    And again, what is maximum Digital I/O speed that can be achieved on the board, which simultaneously samples analog inputs too. Any buffer problem (On-Board (8192 Samples!)) or RAM problem?
    Attempt to solve any of these Questions are appreciated.

    dhruvish,
    Please see the posts at the following thread:
    http://exchange.ni.com/servlet/ProcessRequest?RHIVEID=101&RPAGEID=135&HOID=506500000008000000F8DF0000&UCATEGORY_0=_30_%24_12_&UCATEGORY_S=0
    Ames
    Applications Engineering
    National Instruments

  • How fast does Apple produce new RAW files for new cameras? I want to buy a D7100 wich is not supported in the present version?

    How fast does Apple produce new RAW files for new cameras? I want to buy a D7100 wich is not supported in the present version?

    Jeannot7 wrote:
    I'm interested in seeing who gets the D7100 support first? Apple or Adobe!! I will go with the one that gets it out first.
    FWIW,
    Adobe keeps a quarterly release schedule for RAW updates. The current Lightroom 4.4 RC expires on 5/31/2013 which typically is an indication that the official release will be close to that date. According to the current list of new supported cameras, the Nikon D7100 is not slated to be supported (but that could change on release).
    http://labs.adobe.com/technologies/lightroom4-4/?tabID=details#tabTop
    The most likely time frame for Adobe support of the D7100 will be sometime in August or September 2013 (although there will probably be a Lightroom 4.5 RC available prior to that for testing purposes).

  • How fast can shared variable update?

    Hi,
         According to online documents shared variable engine update at 10ms or forced by flushing command. But  I tested the following two program and can not get the shared variable updated:
          Thanks fo rhelp!
          Tom
    Attachments:
    test_program1.png ‏20 KB
    test_program2.png ‏20 KB

    Hi,
         I don' use NSV to transfer large amount of data between RT and host. The actual application is as follows:
          I also would like to test how fast the NSV update rate and how the flush vi works. I test two programs and did not match the number in the documents.
        Tom

  • How fast text search field in Oracle without using Intermedia?

    How fast text search field in Oracle without using Intermedia? Thank you, Paul.

    yes,it is overriden in VOImpl
    public void executeQuery()
            setQuery((new StringBuilder()).append(selectStmt).append(" order by ").append(getOrderByClause()).toString());
            OAApplicationModuleImpl oaapplicationmoduleimpl = (OAApplicationModuleImpl)getApplicationModule();
            OAApplicationModuleImpl _tmp = oaapplicationmoduleimpl;
            if(oaapplicationmoduleimpl.isLoggingEnabled(1))
                OAApplicationModuleImpl _tmp1 = oaapplicationmoduleimpl;
                oaapplicationmoduleimpl.writeDiagnostics((new StringBuilder()).append(getClass().getName()).append(".executeQuery").toString(), (new StringBuilder()).append(" Query:").append(getQuery()).toString(), 1);
            super.executeQuery();
    But I have extended VO and substituted the VO . In the substituted VOImpl, instead of executeQuery(),I have written
    public void customExecuteQuery()
              setQuery((new StringBuilder()).append(selectStmt).append(" order by ").append(getOrderByClause()).toString());
              executeQuery();
    Will this work,or do I need to do any changes?
    Thanks,

  • How fast/well does GB2/3 run on a Dual-Core Intel Xeon Processor Mac Pro

    never bothered to ask this before but it finally came to me.
    Does anyone who had G5's ( Quad-4.5gb RAM )& who now own the Dual Core Mac Pro souped up notice major improvements in how GB2/3 operates? i mean as far as:
    1- speed
    2- allowing for more midi tracks/audio/loops/etc
    what is the difference with the new Dual Core MACS in terms of how GB works? and therefore why should someone invest in the Mac Pro?
    i.e. will there be a major speed bump with ilife from a Quad to the new intel dual cores?

    I did not ever have a PowerPC Mac nor I have a Mac Pro (which is 4 core, not 2), but anyway GB as of version 3.0.4 is not optimized for multiple cores. This means it will only use 1 core of no matter how many you have on your system. This said, it should anyway quite a bit faster on a Xeon Mac Pro than on a G5 PowerMac, but not sure how faster. Also the number of audio tracks you can have mostly depends on the hard drive speed, even a week G4 CPU can do like 40-50 stereo tracks if the drive is fast enough.

  • How fast is Macbook Air 11 inch wifi?

    Hi
    I have a Macbook air 11 inch 2015 and Asus ac87u router.
    I alway getting 867mbps when i connected to the router, I thought AC could go up to 1gbps+.
    How fast can the macbook air go?
    Thanks

    1Gbps is a theoretical speed - you aren’t going to get that speed and what you are getting is among the fastest I’ve seen. There are several things that bring you below the theoretical speed which includes inference, congestion, how far you are from the router, the number of connections to the router, and network ‘housekeeping’.

  • In terms of percentage, how faster the MBPR 15" is comparing to MBPR  13" ?

    In terms of percentage, how faster the MBPR 15" is comaparing to MBPR  13" ?

    The 15" is 62% faster than the 13"
    http://browser.primatelabs.com/mac-benchmarks

  • How fast is MathScript

    How fast is Mathscript compared to calling a c program using Call Library Function?  In general, Labview is pretty slow in executing itterative algorithms. I get around this by writing these algorithms in C and calling them from Labview. How fast will Mathscript be compared to calling C code.

    We don't currently have any benchmarks now in this regard, so we can't give you a quantitative answer right now.
    Nonetheless, MathScript, like LabVIEW itself, is compiled for
    efficient execution. MathScript execution is fast when the algorithm is
    dominated by computational complexity. However, loop overhead and
    scripts that are not vector- or matrix-based will decrease the
    execution speed.
    We'd be happy to take suggestions for any specific benchmarks that you'd like to see--please let me know.
    Sincerely,
    --Sam
    Sam Shearman

  • How fast can Broadcom 802.11n support for maximum?

    hey everybody,i got U160 yesterday that the WLAN adapter is  Broadcom 802.11n but i just got a connection speed of 65Mbps but i can get 150 Mbps with my G230 with intel 5100 bgn ,connecting to the same N-Router,so i am wondering how fast can Broadcom 802.11n support for maximum.......

    if readycomm installed, remove it.
    check your firewall is active and using high performance.
    by the way, which model of broadcom are you using?

  • How fast can i surf with my iPhone 5 without LTE?

    I living ind Germany and I want to know how fast I can surf without being at the Telekom?

    depend on your carrier  168 Mbit/s seems to be max
    http://en.wikipedia.org/wiki/HSPA%2B

  • IPad notification...stating iCloud could no longer store info.?? how fast does this fill up? And, what should I store /or not store...to keep this from happening?

    iPad notification...stating iCloud could no longer store info.?? how fast does this fill up? And, what should I store /or not store...to keep this from happening?

    Depends on what your storing.  This article will help you see what's using your storage so you can make some decisions on how to reduce it: http://support.apple.com/kb/ht4847.

Maybe you are looking for