compiler-optimization

My C++ object file is too big

て烟熏妆下的殇ゞ 提交于 2019-12-07 08:00:56
问题 I am working on a C++ program and the compiled object code from a single 1200-line file (which initializes a rather complex state machine) comes out to nearly a megabyte. What could be making the file so large? Is there a way I can find what takes space inside the object file? 回答1: There can be several reasons when object files are bigger than they have to be at minimum: statically including dependent libraries building with debug information building with profiling information creating

for loop being ignored (optimized?) out

非 Y 不嫁゛ 提交于 2019-12-07 03:41:32
问题 I am using for/while loops for implementing a delay in my code. The duration of the delay is unimportant here though it is sufficiently large to be noticeable. Here is the code snippet. uint32_t i; // Do something useful for (i = 0; i < 50000000U; ++i) {} // Do something useful The issue I am observing is that this for loop won't get executed. It probably gets ignored/optimized by the compiler. However, if I qualify the loop counter i by volatile, the for loop seems to execute and I do notice

Java compiler optimization for repeated method calls?

不问归期 提交于 2019-12-07 01:42:35
问题 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

Configuration for optimizing QtCreator compiler

纵饮孤独 提交于 2019-12-06 20:19:11
问题 I'm using QtCreator in Windows 7. I want to configure it to use third level optimization ( -O3 ) for c++ compiler. How can I do this to speed up my code and what changes are necessary? 回答1: Try to add next lines to your .pro file: # remove possible other optimization flags QMAKE_CXXFLAGS_RELEASE -= -O QMAKE_CXXFLAGS_RELEASE -= -O1 QMAKE_CXXFLAGS_RELEASE -= -O2 # add the desired -O3 if not present QMAKE_CXXFLAGS_RELEASE *= -O3 回答2: Add these line's to your .pro file if you want a small

How should I detect bottleneck of compile time in a large C++ project?

断了今生、忘了曾经 提交于 2019-12-06 18:30:01
问题 I want to reduce compile time of a large C++ project. I tried to use precompiled headers, interface and etc. But before I move on, I want to know whether any tool which helps detect why compile time is so long. Somebody suggests pc-lint and I will give a shot. How should I detect unnecessary #include files in a large C++ project? But if there are other tools which analysis compile time and talk about any hints to increase compile speed, let me know. Thanks in advance. Environment : Microsoft

Why would a compiler generate this assembly?

不问归期 提交于 2019-12-06 17:12:29
问题 While stepping through some Qt code I came across the following. The function QMainWindowLayout::invalidate() has the following implementation: void QMainWindowLayout::invalidate() { QLayout::invalidate() minSize = szHint = QSize(); } It is compiled to this: <invalidate()> push %rbx <invalidate()+1> mov %rdi,%rbx <invalidate()+4> callq 0x7ffff4fd9090 <QLayout::invalidate()> <invalidate()+9> movl $0xffffffff,0x564(%rbx) <invalidate()+19> movl $0xffffffff,0x568(%rbx) <invalidate()+29> mov 0x564

Can atomic loads be merged in the C++ memory model?

≡放荡痞女 提交于 2019-12-06 17:04:03
问题 Consider the C++ 11 snippet below. For GCC and clang this compiles to two (sequentially consistent) loads of foo. (Editor's note: compilers do not optimize atomics, see this Q&A for more details, especially http://wg21.link/n4455 standards discussion about the problems this could create which the standard doesn't give programmers tools to work around. This language-lawyer Q&A is about the current standard, not what compilers do.) Does the C++ memory model allow the compiler to merge these two

GCC optimization levels. Which is better?

狂风中的少年 提交于 2019-12-06 13:01:19
问题 I am focusing on the CPU/memory consumption of compiled programs by GCC. Executing code compiled with O3 is it always so greedy in term of resources ? Is there any scientific reference or specification that shows the difference of Mem/cpu consumption of different levels? People working on this problem often focus on the impact of these optimizations on the execution time, compiled code size, energy. However, I can't find too much work talking about resource consumption (by enabling

How does clang manage to compile this code with undefined behavior into this machine code?

你说的曾经没有我的故事 提交于 2019-12-06 12:52:23
It's a variation of code from this tweet , just shorter one and not causing any damage to noobs. We have this code: typedef int (*Function)(); static Function DoSmth; static int Return7() { return 7; } void NeverCalled() { DoSmth = Return7; } int main() { return DoSmth(); } You see that NeverCalled() is never called in the code, don't you? Here's what Compiler Explorer shows when clang 3.8 is selected with -Os -std=c++11 -Wall Code emitted is: NeverCalled(): retq main: movl $7, %eax retq as if NeverCalled() was actually called before DoSmth() and set the DoSmth function pointer to Return7()

Algorithm Code Optimization: Find the Equilibirum: Find an index in an array such that its prefix sum equals its suffix sum

岁酱吖の 提交于 2019-12-06 09:57:10
问题 Here's my Task Description: A zero-indexed array A consisting of N integers is given. An equilibrium index of this array is any integer P such that 0 ≤ P < N and the sum of elements of lower indices is equal to the sum of elements of higher indices, i.e. A[0] + A1 + ... + A[P−1] = A[P+1] + ... + A[N−2] + A[N−1]. Sum of zero elements is assumed to be equal to 0. This can happen if P = 0 or if P = N−1. For example, consider the following array A consisting of N = 8 elements: A[0] = -1 A[1] = 3