Is performance gained when using continue in a for-loop with many if-statements?

后端 未结 4 1649
小蘑菇
小蘑菇 2021-01-19 11:12

I have a for loop in a java program which iterates through a set of maps.

Inside the loop I have around 10 different if-statements which checks the name of each key

4条回答
  •  爱一瞬间的悲伤
    2021-01-19 11:13

    Because you have just a few values, IMO, you'll have a real performance improvement here if you map your strings to ints, since the int comparison is far faster than a String comparison.

    Check this out

    public class Lab1 {
    
        public static void main(String[] args) {
    
            usingStrings();
            usingInts();
    
    
        }
    
        private static void usingInts() {
            int[] samples = new int[100000000];
            int[] values = {1,2,3,4};
            for(int i=0;i
                                                            
提交回复
热议问题