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

后端 未结 21 2074
别那么骄傲
别那么骄傲 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 05:05

    Use the length of the code to create a unique value from that code instead of using GetHashCode() . It turns out there are no collisions if you use the first letter of the code shifted by the length of the code. This reduces the cost to two comparisons, one array index and one shift (on average).

    public static MarketDataExchange GetMarketDataExchange(string ActivCode)
    {
        if (ActivCode == null)
            return MarketDataExchange.NONE;
        if (ActivCode.Length == 0)
            return MarketDataExchange.NBBO;
        return (MarketDataExchange)((ActivCode[0] << ActivCode.Length));
    }
    
    public enum MarketDataExchange
    {
        NONE = 0,
        NBBO = 1,
        AMEX = ('A'<<1),
        BSE = ('B'<<1),
        BATS = ('B'<<2),
        NSE = ('C'<<1),
        CHX = ('M'<<2),
        NYSE = ('N'<<1),
        ARCA = ('P'<<2),
        NASDAQ = ('Q'<<1),
        NASDAQ_ADF = ('Q'<<2),
        CBOE = ('W'<<1),
        PHLX = ('X'<<1),
        DIRECTEDGE = ('Y'<<1),
    }
    

提交回复
热议问题