天道酬勤,学无止境

switch-statement

Switch 语句中的大小写顺序是否会影响性能?(Does the order of case in Switch statement can vary the performance?)

问题 假设我有一个 switch 语句,如下所示 switch(alphabet) { case "f": //do something break; case "c": //do something break; case "a": //do something break; case "e": //do something break; } 现在假设我知道Alphabet e 的频率最高,其次是 a、c 和 f。 所以,我只是重组了case语句的顺序,并按如下方式制作: switch(alphabet) { case "e": //do something break; case "a": //do something break; case "c": //do something break; case "f": //do something break; } 第二个switch语句会比第一个switch语句快吗? 如果是,如果在我的程序中我需要多次调用这个switch语句,这会是一个实质性的改进吗? 或者,如果没有,我如何使用我的频率知识来提高性能? 回答1 没有那么多,你应该担心。 这当然不是可以预测的。 对于字符串大小写标签,编译器实际上使用内部哈希表将字符串映射到跳转表中的索引。 所以操作实际上是 O(1) - 与标签的数量无关。 对于整数标签

2021-06-21 12:21:17    分类:技术分享    c#   performance   micro-optimization   switch-statement

在 Listview Android 中滚动时切换更改 ON-OFF(Switch changes ON-OFF when scrolled in Listview Android)

问题 我有一个带开关的 ListView。 每当我打开开关并向下滚动并向上滚动时,开关就会变为关闭状态。 回答1 把它放在你的自定义适配器中.. // 当视图被回收时,这两种方法允许在不打开/关闭开关的情况下滚动 // 这些方法实际上告诉适配器不要回收行,因此可能会减慢进程 @Override public int getViewTypeCount() { return getCount(); } @Override public int getItemViewType(int position) { return position; } 回答2 我在ListView遇到了这个问题。 试试这个 if(checkedItems.indexOf(accountList.get(position).toString()) >= 0) { holder.switchItem.setChecked(true); } else { holder.switchItem.setChecked(false); } 您正在使用它之前初始化checkedItems。 它目前是空的。 当 indexOf 中没有任何内容时,如何执行它。 这应该可以解决您的问题 回答3 SparseBooleanArray没有任何名为indexOf方法。 如果您只想存储已检查的状态

2021-06-21 12:06:51    分类:技术分享    android   listview   switch-statement

switch for type string for java 1.6 [duplicate]

This question already has answers here: Why can't I use switch statement on a String? (17 answers) Closed 8 years ago. I have the following code and I'm wondering if there is a way to use a switch block instead of a bunch of if/else statements. I know that Java supports strings in switch blocks as of Java 1.7 but I'm still working with Java 1.6: } else if (typeName.equals("Boolean")) { return new SwitchInputType<Boolean>(new Boolean((String) memberValue)); } else if (typeName.equals("Double")) { return new SwitchInputType<Double>(new Double((String) memberValue)); } else if (typeName.equals(

2021-06-15 12:45:47    分类:问答    java   switch-statement

How can I use goto in a switch statement in Objective-C?

In my code I need to be able to jump (goto) a different case within the same switch statement. Is there a way to do this? My code is something like this: (There is a lot of code I just left it all out) switch (viewNumber) { case 500: // [...] break; case 501: // [...] break; . . . . . case 510: // [...] break; default: break; } Thank you for your time! -Jeff

2021-06-15 07:33:31    分类:问答    iphone   objective-c   switch-statement   goto

VB.NET How give best performance “Select case” or IF… ELSEIF … ELSE… END IF

I have a huge DataTable, and I need go by each row and validate an specific value. Which method give me more performance, an structure of IF ELSE or SELECT CASE? (I'm focused in the method that offer me the best performance) IF ELSE (METHOD #1) For Each vRow In vDTtemp.Rows If vRow("Item") = "Time" Then vRow("Result") = "000" ElseIf vRow("Item") = "DateTime" Then vRow("Result") = "001" ElseIf vRow("Item") = "String" Then vRow("Result") = "002" Else vRow("Result") = "N/A" End If Next SELECT CASE (METHOD #2) For Each vRow In vDTtemp.Rows Select Case vRow("Item") Case "Time" vRow("Result") = "000

2021-06-14 19:10:42    分类:问答    vb.net   performance   if-statement   switch-statement   cycle

Execute integer templated function based on run-time parameter

I often have some prototype behaviour that generates output based on some design method. I template the design method, which enables a lot of functionality I need. However, sometimes the design method is given at runtime, so I'm usually required to write a huge switch statement. It usually looks like this: enum class Operation { A, B }; template<Operation O> void execute(); template<> void execute<A>() { // ... } template<> void execute<B>() { // ... } void execute(Operation o) { switch (o) { case Operation::A: return execute<Operation::A>(); case Operation::B: return execute<Operation::B>();

2021-06-14 17:08:07    分类:问答    c++   switch-statement   metaprogramming

Nested 'if'-'else' statements

I have code that's very messy with the if - else if checks it is doing. The amount of branching and nested branching is quite big (over 20 if - else if and nested too). It's making my code harder to read and will probably be a performance hog. My application checks for a lot of conditions it gets from the user and so the application must check all the time for different situations, for example: If the textbox text is not 0, continue with the next... if ((StartInt != 0) && (EndInt != 0)) { And then here it checks to see whether the user have choosen dates: if ((datePickerStart.SelectedDate ==

2021-06-14 15:32:09    分类:问答    c#   switch-statement   if-statement

Make compiler assume that all cases are handled in switch without default

Let's start with some code. This is an extremely simplified version of my program. #include <stdint.h> volatile uint16_t dummyColorRecepient; void updateColor(const uint8_t iteration) { uint16_t colorData; switch(iteration) { case 0: colorData = 123; break; case 1: colorData = 234; break; case 2: colorData = 345; break; } dummyColorRecepient = colorData; } // dummy main function int main() { uint8_t iteration = 0; while (true) { updateColor(iteration); if (++iteration == 3) iteration = 0; } } The program compiles with a warning: ./test.cpp: In function ‘void updateColor(uint8_t)’: ./test.cpp

2021-06-14 15:29:34    分类:问答    c++   switch-statement   g++

enum referencing in switch case

enum Color {RED, GREEN, BLUE}; class SwitchEnum { public static void main(String[] args) { Color c = Color.GREEN; switch(c) { case RED: System.out.println("red"); break; case GREEN: System.out.println("green"); break; case BLUE: System.out.println("blue"); break; } } } The above code compiles fine and gives the expected output. My question is why when creating the Color reference 'c' we needed to refer it through the name of the enum (i.e. Color.GREEN) but in the case block only the enum value sufficed. Shouldn't it have been case Color.RED: etc???

2021-06-14 13:49:36    分类:问答    java   enums   switch-statement

python - Simulating 'else' in dictionary switch statements

I'm working on a project which used a load of If, Elif, Elif, ...Else structures, which I later changed for switch-like statements, as shown here and here. How would I go about adding a general "Hey, that option doesn't exist" case similar to an Else in an If, Elif, Else statement - something that gets executed if none of the Ifs or Elifs get to run?

2021-06-14 13:20:12    分类:问答    python   switch-statement   case   if-statement