compiler-optimization

How can I prevent the gcc optimizer from producing incorrect bit operations?

…衆ロ難τιáo~ 提交于 2019-12-05 08:47:16
问题 Consider the following program. #include <stdio.h> int negative(int A) { return (A & 0x80000000) != 0; } int divide(int A, int B) { printf("A = %d\n", A); printf("negative(A) = %d\n", negative(A)); if (negative(A)) { A = ~A + 1; printf("A = %d\n", A); printf("negative(A) = %d\n", negative(A)); } if (A < B) return 0; return 1; } int main(){ divide(-2147483648, -1); } When it is compiled without compiler optimizations, it produces expected results. gcc -Wall -Werror -g -o TestNegative

Which gcc optimization flags should I use?

為{幸葍}努か 提交于 2019-12-05 06:49:05
问题 If I want to minimize the time my c programs run, what optimization flags should I use (I want to keep it standard too) Currently I'm using: -Wall -Wextra -pedantic -ansi -O3 Should I also use -std=c99 for example? And is there I specific order I should put those flags on my makefile? Does it make any difference? And also, is there any reason not to use all the optimization flags I can find? do they ever counter eachother or something like that? 回答1: I'd recommend compiling new code with -std

Java compiler optimization for repeated method calls?

孤街醉人 提交于 2019-12-05 06:16:34
Does the java compiler (the default javac that comes in JDK1.6.0_21) optimize code to prevent the same method from being called with the same arguments over and over? If I wrote this code: public class FooBar { public static void main(String[] args) { foo(bar); foo(bar); foo(bar); } } Would the method foo(bar) only run once? If so, is there any way to prevent this optimization? (I'm trying to compare runtime for two algos, one iterative and one comparative, and I want to call them a bunch of times to get a representative sample) Any insight would be much appreciated; I took this problem to the

Visual C++ Compiler Optimization Flags: Difference Between /O2 and /Ot

你。 提交于 2019-12-05 05:43:41
What's the difference between the /Ot flag ("favor fast code") and the /O2 flag ("maximize speed")? (Ditto with /Os and /O1 .) /O1 and /O2 bundle together a number of options aimed at a larger goal. So /O1 makes a number of code generation choices that favour size; /O2 does the same thing and favours speed. /O1 includes /Os as well as other options. /O2 includes /Ot as well as other options. Some optimisations are enabled by both /O1 and /O2. And, depending on your program's paging behaviour, /O1 (size) can result in faster speed than /O2 if paging code comes to dominate your perf over

Java optimizer and redundant array evaluations

筅森魡賤 提交于 2019-12-05 05:36:43
This is a very basic question about the Java optimization. If you have a simple for loop to iterate through an array and use array.length in the header of the loop rather than evaluating it before so that you do it only once (which is what I almost always do): for(int i=0; i<array.length;i++) { ... } Can the statement be optimized so that the JVM knows whether the array is changing for the duration of the loop so that it does not reevaluate array.length every time? if another thread is not modifying the array concurrently, will array.length be effectively evaluated only once, More critically,

Is Clang really this smart?

孤街醉人 提交于 2019-12-05 04:05:41
If I compile the following code with Clang 3.3 using -O3 -fno-vectorize I get the same assembly output even if I remove the commented line. The code type puns all possible 32-bit integers to floats and counts the ones in a [0, 1] range. Is Clang's optimizer actually smart enough to realize that 0xFFFFFFFF when punned to float is not in the range [0, 1], so ignore the second call to fn entirely? GCC produces different code when the second call is removed. #include <limits> #include <cstring> #include <cstdint> template <class TO, class FROM> inline TO punning_cast(const FROM &input) { TO out;

Why isn't string concatenation automatically converted to StringBuilder in C#? [duplicate]

梦想的初衷 提交于 2019-12-05 04:03:06
Possible Duplicate: Why is String.Concat not optimized to StringBuilder.Append? One day I was ranting about a particular Telerik control to a friend of mine. I told him that it took several seconds to generate a controls tree, and after profiling I found out that it is using a string concatenation in a loop instead of a StringBuilder. After rewriting it worked almost instantaneously. So my friend heard that and seemed to be surprised that the C# compiler didn't do that conversion automatically like the Java compiler does. Reading many of Eric Lippert's answers I realize that this feature didn

Compiler optimization makes program crash

不打扰是莪最后的温柔 提交于 2019-12-05 01:55:29
问题 I'm writing a program in C++/Qt which contains a graph file parser. I use g++ to compile the project. While developing, I am constantly comparing the performance of my low level parser layer between different compiler flags regarding optimization and debug information, plus Qt's debug flag (turning on/off qDebug() and Q_ASSERT()). Now I'm facing a problem where the only correctly functioning build is the one without any optimization . All other versions, even with -O1 , seem to work in

Could a Java compiler reorder function calls?

丶灬走出姿态 提交于 2019-12-05 01:29:58
I know that java compiler can actually reorder code instructions. But can java reorder function calls? For example: ... //these lines may be reordered a=7; b=5; ... //but what about this? callOne(); callTwo(); If it can determine that doing so would have no effect on the result, then yes. Since it can't, the compiler won't. JIT can however inline the calls, since it knows if the methods are overridden, and it can then rearrange the code, if it sees fit. Since it can only do so if it can guarantee that result stays the same, why would you even care? You can't see a difference anyway. For a

Can a conforming C# compiler optimize away a local (but unused) variable if it is the only strong reference to an object?

爷,独闯天下 提交于 2019-12-05 00:57:19
See also these related resources: Does the .NET garbage collector perform predictive analysis of code? (on Stack Overflow) WP7: When does GC Consider a Local Variable as Garbage (blog article on MSDN) In other words: Can an object referenced by a local variable be reclaimed before the variable goes out of scope (eg. because the variable is assigned, but then not used again), or is that object guaranteed to be ineligible for garbage collection until the variable goes out of scope? Let me explain: void Case_1() { var weakRef = new WeakReference(new object()); GC.Collect(); // <-- doesn't have to