How would you make this switch statement as fast as possible?

后端 未结 21 2039
别那么骄傲
别那么骄傲 2021-01-30 04:17

2009-12-04 UPDATE: For profiling results on a number of the suggestions posted here, see below!


The Question

Consider the following very

21条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-30 04:51

    Messy but using a combination of nested ifs and hard coding might just beat the optimiser:-

       if (ActivCode < "N") {
             // "" to "MW"
             if (ActiveCode < "BT") {
                // "" to "B"
                if (ActiveCode < "B") {
                    // "" or "A"
                    if (ActiveCode < "A") {
                          // must be ""
                         retrun MarketDataExchange.NBBO;
                    } else {
                         // must be "A"
                        return MarketDataExchange.AMEX;
                    }
                } else {
                    // must be "B"
                    return MarketDataExchange.BSE;
                }
             } else {
                // "BT" to "MW"
                if (ActiveCode < "MW") {
                    // "BT" or "C"
                    if (ActiveCode < "C") {
                          // must be "BT"
                         retrun MarketDataExchange.NBBO;
                    } else {
                         // must be "C"
                        return MarketDataExchange.NSE;
                    }
                } else {
                // must be "MV"
                    return MarketDataExchange.CHX;
                }
             }
        } else {
            // "N" TO "Y"
             if (ActiveCode < "QD") {
                // "N" to "Q"
                if (ActiveCode < "Q") {
                    // "N" or "PA"
                    if (ActiveCode < "PA") {
                          // must be "N"
                         retrun MarketDataExchange.NYSE;
                    } else {
                         // must be "PA"
                        return MarketDataExchange.ARCA;
                    }
                } else {
                    // must be "Q"
                    return MarketDataExchange.NASDAQ;
                }
             } else {
                // "QD" to "Y"
                if (ActiveCode < "X") {
                    // "QD" or "W"
                    if (ActiveCode < "W") {
                          // must be "QD"
                         retrun MarketDataExchange.NASDAQ_ADF;
                    } else {
                         // must be "W"
                        return MarketDataExchange.CBOE;
                    }
                } else {
                // "X" or "Y"
                    if (ActiveCode < "Y") {
                          // must be "X"
                         retrun MarketDataExchange.PHLX;
                    } else {
                         // must be "Y"
                        return MarketDataExchange.DIRECTEDGE;
                    }
                }
             }
        }
    

    This gets the right function with three or four compares. I wouldnt even think of doing this for real unless your piece of code is expected to run several times a second!

    You further otimise it so that only single character compares occurred. e.g. replace '< "BT" ' with '>= "B" ' -- ever so slightly faster and even less readable!

提交回复
热议问题