Tag Info

New answers tagged

1

Yes, the condition is checked either way. But the advantage of branch prediction is that you can do work instead of waiting for the result of the condition check. Lets say you have to write an essay and it can be about topic A or topic B. You know from previous essays that your teacher likes topic A better than B and chooses it more often. Instead of ...


1

Short form: Some CPUs can start working on a new instruction before finishing the old one. These are the CPUs that use branch prediction. A pseudocode example: int globalVariable; int Read(int* readThis, int* readThat) { if ((globalVariable*globalVariable % 17) < 5) return *readThis; else return *readThat; } The above code ...


8

Think of it like a road trip without GPS. You come to an intersection, and think you need to turn, but aren't completely sure. So you take the turn, but ask your passenger to check the map. Maybe you're three miles down the road by the time you finish arguing about where you are. If you were right, you're three miles farther than you would have been if ...


2

From my understanding, branch prediction is most useful when the condition you need to check requires the outcome of something which is expensive or still in progress, and you would otherwise be twiddling your thumbs waiting for the value to evaluate the condition. With things like out-of-order execution, you can use branch prediction to start filling in ...


9

Of course the condition is checked every single time. But by the time it is checked, it is far up into the CPU pipeline. In the mean time, other instructions have also entered the pipeline, and are at various stages of execution. Usually, a condition is immediately followed by a conditional branch instruction, which either branches if the condition ...


4

You cannot write only one function. You will still need to have a separate function for each event handler, so the best you can do is to have 3 functions whose total amount of code will be less than what you currently have because it will not contain duplicated code. It will not perform faster, but it will be smaller. So, what you need to do is to extract ...


0

I don't think any do, but it should be possible. For example gcc 4.8 (scroll down to IA-32/x86-64 section) has both a builtin __builtin_cpu_supports function and function multiversioning support that the developer can use to write different code for different architectures. I don't see why the compiler couldn't generate different codepaths dependant on the ...


5

For a compiler to do run time checks (to find warnings and errors that cannot be found at compile time, including loop vectorization), it starts to get dangerously close to the Halting Problem. Alan Turing proved in 1936 that a general algorithm to solve the halting problem for all possible program-input pairs cannot exist. A key part of the proof was a ...


6

My question is 1) is this true No, this is complete and utter nonsense. Automatic parallelization of code that wasn't explicitly written to be parallel has been (one of) the holy grail(s) of optimizers for decades, but it still doesn't work for anything but the most trivial cases. Even just figuring out whether a piece of code has side-effects at all, ...


1

Answer to 1 --its not true! There are very few(none really) compilers/interpreters clever enough to recognise that a calculation can be split into parallel blocks without affecting the result in some way. Several languages have support for parallel computation. Most modern C/C++ and FORTRAN support the OpenMP API which allows the programmer control of ...


3

This is a very common question for any programming language, and the answer is surprisingly simple. Do what makes sense and makes the program better. Here are qualities that make a program/script better in no particular order: Ease of understanding Ease of maintenance Ease of extension Ability to be tested and debugged Portability Performance If ...



Top 50 recent answers are included