Warning: sizeof(): Parameter must be an array or an object that implements Countable in /home/helpmekim2dhsefl3pwmseak8ismo2/wwwroot/wp-content/plugins/ad-injection/ad-injection.php on line 824 Warning: count(): Parameter must be an array or an object that implements Countable in /home/helpmekim2dhsefl3pwmseak8ismo2/wwwroot/wp-content/plugins/ad-injection/ad-injection.php on line 831

The question is not specific enough, “full of if-else branches”, does it mean that there are many branches at the same level in the same function? Or is there a lot of branches in the same file or module? Or is the nesting level of branches very deep? Say there are many branches, how many are there? tens of? If there are many branches of the same function at the same level, it means that there are too many variables used to judge the condition in the logic expression. Otherwise, if there are many values ​​of a variable, as long as it is enumerable, switch-case should be given priority. When the variable of the judgment condition is one, there are two choices; when there are two variables, there are four combinations, up to four branches; and so on, as the number of variables increases, the number of branches increases exponentially. Anyone who has studied algorithm complexity analysis knows that any exponential consumption, regardless of time or space, is a disaster for the current Turing computer. However, the exponential growth of the if-else branch will not have much impact on the operating efficiency of the program, and it is estimated that the variables of the judgment condition will not exceed five. The operating efficiency of the program is often more related to the number of loops, the number of loop nesting, the depth of the call stack, and the blockage of read and write. One more branch statement is just to do a few more logical expression operations and program pointer jumps, not to mention the short-circuit optimization of logical expressions, which will not cause a bottleneck in operating efficiency. The exponential growth of the if-else branch is more a test of human logic. People like to pursue great unification, and hope that the four forces can best share a theoretical model, because this is in line with people’s nature to pursue simplicity in cognition. When you say “except for maintainability”, you seem to have a dismissive tone. It seems that only the efficiency of the program is important. This is a bad attitude that many junior programmers tend to hold. To say that poor maintainability is just the appearance, and the inner description is the design of the program when coding: the model is not concise enough, the boundary is sloppy, and the test is not complete. These reflect that the coders are not thinking deeply and adequately. For a function, the more branches inside, the more abnormal situations. The more abnormal situations, it means that the designed model is not universal enough, and it needs to be patched through logic branches. If you try to write test cases, you will find that if you want to cover each line of code, there are several branches, and you have to design several test cases. Ideally, if there is no branch, then only one test case is needed, but in reality, this is impossible, and there will always be various abnormal situations. For example, anyone who has brushed LeetCode knows that the realization of a string-to-integer function (atoi) is a simple-sounding problem, but the pass rate is not high. The fundamental reason is that most people cannot fully understand The possible exceptions in the string are very clear. Many abnormal situations are the main reason for the many branches, but in the case of in-depth thinking, the number of branches can still be controlled to the right number, and the top-down decomposition problem is the main method to solve the confusion of branches. People who have studied calculus probably like elementary functions that can be derived everywhere, but they don’t have a good impression of piecewise functions. In order to solve the problem that programmers do not think deeply, test-driven development is advocated in the industry. When a person starts to look at the function written by himself from the perspective of a user, if he wants to achieve high code coverage, he will definitely be assisted. Deepen your thinking.

zhiwo

By zhiwo

0 0 vote
Article Rating
Subscribe
Notify of
guest
11 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
helpmekim
8 months ago

Many answers have talked about the impact of the cpu side, because there are branch predictions, the impact may not be very large. Let me talk about the impact of the cuda program on the gpu side. Because SM does not have branch prediction, if there are branches in the kernel, it may cause part of the threads in the warp to be executing and part of them waiting (regardless of whether the condition is established or not, they must be executed), so the utilization rate is very low. This problem is called the warp differentiation problem.

heloword
8 months ago

At the CPU level, there is no if else. Some just set some flags according to the calculation results, and then decide whether to jump according to this flag. That is, whether it is if else, or for, or while, or switch, when it comes to the CPU machine code level, it is actually a conditional jump, and there is not much difference. The main problem of if-else flooding in the code is to make the code less readable and maintainability worse. Many beginners think that the code is written for computers. Can’t be wrong. But if this is the case, then it is enough to have the paper tape that Bill Gates hit on the plane (that is, to write the machine code directly), or at most a compilation. Because for computers, they are the easiest to understand and clearest (unambiguous) language. The invention of language such as C, or more advanced high-level languages, is actually for the convenience of people to see, not computers. Computers have to understand high-level languages. It takes a lot of hard work. It takes a lot of computing power to interpret or compile programs written in these high-level languages. Misunderstandings often occur. Therefore, poor readability/maintainability is bad enough for programs written in high-level languages.

helpyme
8 months ago

The bottleneck of the actual project is by no means 10 million or 1 million executions per second. Coordination and maintenance between systems in actual projects is the bottleneck. For example, a software system A contains subsystem a, which is outsourced by company 1, subsystem b, which is outsourced by company 2, and subsystem c is outsourced by company 3. In addition, system A includes system B cross-system calls and is outsourced by a central system C Control. It is meaningless to talk about instruction-level optimization at this time. At this time, your code can be quickly read by other people or yourself, and you can quickly find and modify the problem, which is the most important problem. And if you write too much, I’m afraid I can’t even read it myself. If there are more branches, design patterns must be involved. For example, some counterexamples that I often come into contact with are copying and pasting code without even a basic package. In the end, no one can touch this code. Especially in large companies, the personnel mobility is relatively high. It is a disaster to get the code back to the next person or the next person.

sina156
8 months ago

If there are too many if else, it is often because of poor business understanding, poor implementation of local logic, or operation and maintenance for many years. If there are too few if else, it is often the business implementation is rough, without considering the boundary, or the implementation is very clever, or the programmer’s obsession is too deep, and the interface must be dynamic. This may affect performance. In fact, there is nothing wrong with it. If you have more, just see if you think it is wrong. If you have less, then think about what you didn’t expect. This is a balance.

yahoo898
8 months ago

An else is a branch. If you have more elses, your mind will have to deal with multiple branch situations; if you put a few else in the else, the branch situation will increase exponentially. Even if the computer deals with programs with exponential complexity, the performance will be greatly reduced. , You still want to maintain this stuff alone? Do you think your brain ages fast enough? Unless you are ready to mechanically ascend, or you should treat yourself a little better, and you can use it for decades. One period and one time, cherish the present.

leexin
8 months ago

I really don’t want to answer this question. Okay, let’s just say it briefly. Originally a function can perfectly measure the weight of the fruit and output it according to the metric system. Then one day, a customer said that we should output the results in English system. Customer b said, let’s change the result to another format. Customer c, please output the name of the fruit. Customer d, please output the color of the fruit. But don’t output the name. Would you say I reconstruct it? The leader said, shouldn’t you solve this problem in five minutes? So you say if else affects efficiency, is it useful?

greatword
8 months ago

In fact, the emergence of various “smart” compilers makes code writing and hardware execution “seriously out of touch”. So I think the execution efficiency depends on the code optimization of the interpreter and compiler. For example, analyzing the execution efficiency of a piece of Java code will eventually fall on the implementation of jvm. Branch prediction, instruction rearrangement, pipeline, cache, hot code, etc. will affect the execution efficiency; in fact, many high praise answers mention code readability Relatively more important than execution efficiency (in most cases, but not absolute, affected by objective factors). In addition, the questioner’s description of the question made me think that he understands the readability of if-else, so I won’t say much. In addition, there are many answers that mentioned the complexity of the problem. I have a desire to tell this: classic (many not classic. ..) There are 3 basic control processes mentioned in basic computer textbooks: sequence, loop, and selection. I think from the perspective of magic and magic, this should be magic. The if-else is the most direct embodiment of the selection process, and the complexity of the logic is obvious. But through some “methods”, such as design patterns, polymorphism, method extraction, etc. (essentially these are different organization forms of code), the logic complexity can be dispersed or transferred. However, the complexity cannot be eliminated. If the problem can be directly attributed to the impact of the selection process implementation method on the execution efficiency, the impact is not significant. But there is still a layer of interpreter or compiler between the code and the machine code (it seems like Turing said that adding a layer can solve all the problems…now the layer is so high and headache), so now the choice of process implementation has an impact on execution efficiency , It also depends on how much complexity the middle layer bears (transferred to it), which implementation is more friendly, such as supporting cache branches for all implementations, trading space for time, and improving execution efficiency. Most of the time, the more complex the middle layer, the higher the execution efficiency of the generated machine code

loveyou
8 months ago

This undoubtedly introduces a large number of branches. When the processor branch prediction fails, the processor will be forced to empty the pipeline. Of course, the penalty caused by it is generally not serious. Moreover, in some cases, the compiler will optimize if else as conditional transmission rather than pure comparison and jump. But it is not a good thing that this happens in the code, especially in the hot code. At the same time, the code is full of if else, which also reflects that there may be problems in other designs in the project, which will more or less affect the whole, such as robustness.

strongman
8 months ago

If-else itself is not very movie performance, it is a very basic process statement, there will be no problems in performance processing. However, it is often seen that because of logic problems, the use of if-else is very weird, resulting in poor performance and even hidden bugs. Commonly, there may be initialized objects. In the end, it is actually useless to initialize or recycle objects that are skipped and not executed in the complex if. Repeated calls to time-consuming functions in if and else if In fact, there is nothing wrong with if itself, but improper use will cause the logic to be unclear and cause logic problems in other codes. Some problems will report exceptions and can still be checked, and some problems cause waste and are not easy to check.

stockin
8 months ago

To give an extreme example, if you need to implement a function in your system to convert the country code into a country name, so you write more than 200 if-else, this function is used 800 times in your system, so you After copying and pasting 800 more, there are 160,000 if-else. There are also 20 similar functional systems, with a total of 3.2 million if-else. Do you want to see if your source code is very large, others are only a few kb, you are in Mb units, will you compile and run slower? If this is a front-end function and no compressed js is used, is your js larger? When your internet speed is not good, will it not be loaded?

11
0
Would love your thoughts, please comment.x
()
x